Thursday 5 December 2013

What is Early Binding and Late Binding in C#.NET and what is the Difference between Early Binding and Late Binding


In this article I will explain what is Early Binding and Late Binding in C#.NET and what is the Difference between Early Binding and Late Binding.

Polymorphism:

An operation may exhibit different behaviours in different instances.Polymorphism means same operation may behave differently on different classes.The behaviour depends on the data types used in the operation.
Polymorphism is extensively used in implementing Inheritance.

 In Polymorphism we have 2 different types those are

       -  Early Binding
       -  Late Binding


 Early Binding

Compile time polymorphism means we will declare methods with same name but different signatures because of this we will perform different tasks with same method name. This compile time polymorphism also called as early binding or method overloading.

Example of Method Early Binding:

      class A1
        {
            void hello()
            {
                Console.WriteLine("Hello");
            }

            void hello(string s)
            {
                Console.WriteLine("Hello {0}", s);
            }
        }

Late Binding

Late binding also called as run time polymorphism or method overriding or dynamic polymorphism. Method overriding or run time polymorphism means same method names with same signatures.

Example of Method Late Binding:

        class parent
        {
            virtual void hello()
            {
                Console.WriteLine("Hello from Parent");
            }
        }

        class child : parent
        {
            override void hello()
            {
                Console.WriteLine("Hello from Child");
            }
        }

        static void main()
        {
            parent objParent = new child();
            objParent.hello();
        }

No comments:

Post a Comment