Compile time polymorphism vs. run time polymorphism

后端 未结 9 2264
栀梦
栀梦 2020-12-23 00:15

Why is overloading called compile time polymorphism and Overriding run time polymorphism in C#?

9条回答
  •  盖世英雄少女心
    2020-12-23 00:46

    Polymorphism

    Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. This is called polymorphism.

    Polymorphism means having more than one form. Overloading and overriding are used to implement polymorphism. Polymorphism is classified into compile time polymorphism or early binding or static binding and Runtime polymorphism or late binding or dynamic binding.

    Overriding - same method names with same arguments and same return types associated in a class and its subclass. Overriding in C# makes use of the "override" keyword. To override a method means to replace it with a new way of handling data.

    Overloading - same method name with different arguments, may or may not be same return type written in the same class itself.

    Compile time Polymorphism or Early Binding

    The polymorphism in which compiler identifies which polymorphic form it has to execute at compile time it self is called as compile time polymorphism or early binding.

    Advantage of early binding is execution will be fast. Because every thing about the method is known to compiler during compilation it self and disadvantage is lack of flexibility.

    Examples of early binding are overloaded methods, overloaded operators and overridden methods that are called directly by using derived objects.

    Runtime Polymorphism or Late Binding

    The polymorphism in which compiler identifies which polymorphic form to execute at runtime but not at compile time is called as runtime polymorphism or late binding.

    Advantage of late binding is flexibility and disadvantage is execution will be slow as compiler has to get the information about the method to execute at runtime.

    Example of late binding is overridden methods that are called using base class object.

     class A
     {
        public virtual void Leg(string Name)
        {
    
        }
     }
    
     class B:A
     {
        public override void Leg(string Name)
        {
    
        }
     }
    

    Example for Over loading

     class A
     {
      void a()
       {
       }
      void a(string Name)
       {
       }
     }
    

    In other words, "Many forms of a single object is called Polymorphism."

    Eg:

    A Team Leader behaves to Sub Ordinate. A Team Leader behaves to his/her seniors. A Team Leader behaves to other Team Leaders.

    Here Team Leader is an object but attitude is different in different situation.

    Difference between Method Overriding and Method hiding

    Method overriding allows a subclass to provide a specific implementation of a method that is already provided by base class. The implementation in the subclass overrides (replaces) the implementation in the base class. The important thing to remember about overriding is that the method that is doing the overriding is related to the method in the base class. When a virtual method is called on a reference, the actual type of the object to which the reference refers is used to determine which method implementation should be used. When a method of a base class is overridden in a derived class (subclass), the version defined in the derived class is used. This is so even should the calling application be unaware that the object is an instance of the derived class.

    Method hiding does not have a relationship between the methods in the base class and derived class. The method in the derived class hides the method in the base class.

提交回复
热议问题