OOP – What is Polymorphism-Static Polymorphism-Dynamic Polymorphism-Runtime Polymorphis-Function overriding-function overloading-Virtual method

What is Polymorphism

  • Polymorphism means one object behaving as multiple forms.
  • One function behaves in different forms.
  • In other words, “Many forms of a single object is called Polymorphism.”

Person behaves as a SON in house, at the same time that person behaves like an EMPLOYEE in the office.
With polymorphism, the same method or property can perform different actions depending on the run-time type of the instance that invokes it.

There are two types of polymorphism:

  • Static or compile time polymorphism
  • Dynamic or runtime polymorphism

Polimorphism, Runtime Polimorphism, Static Polimorphism

Static Polymorphism:

  • The decision is made at compile time.
  • Which method is to be called is decided at compile-time only.
  • Method overloading is an example of this.
  • Compile time polymorphism is method overloading, where the compiler knows which overloaded method it is going to call.
  • Method overloading is a concept where a class can have more than one method with the same name and different parameters.
  • Compiler checks the type and number of parameters passed on to the method and decides which method to call at compile time and it will give an error if there are no methods that match the method signature of the method that is called at compile time.

Dynamic or Runtime Polymorphism:

  • Run-time polymorphism is achieved by method overriding.
  • Method overriding allows us to have methods in the base and derived classes with the same name and the same parameters.
  • By runtime polymorphism, we can point to any derived class from the object of the base class at runtime that shows the ability of runtime binding.
  • Through the reference variable of a base class, the determination of the method to be called is based on the object being referred to by reference variable.
  • Compiler would not be aware whether the method is available for overriding the functionality or not. So compiler would not give any error at compile time. At runtime, it will be decided which method to call and if there is no method at runtime, it will give an error.

Virtual Method:

  • Virtual method is a method whose behavior can be overridden in derived class.
  • Virtual method allows declare a method in base class that can be redefined in each derived class.
  • When a virtual method is invoked, the run-time type of the object is checked for an overriding member.
  • The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.
  • By default, methods are non-virtual. You cannot override a non-virtual method.
  • You cannot use the virtual modifier with the static, abstract, private or override modifiers.
  • Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax.
  • It is an error to use the virtual modifier on a static property.
  • A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.

Virtual method solves the following problem:

  • In OOP, when a derived class inherits from a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class methods overridden by the derived class, the method call behavior is ambiguous.
  • In C#, polymorphism is explicit – you must have a virtual (or abstract) modifier on the base class method (member) and an override on the derived class method, which you probably already know.
  • If you don’t put a modifier on a base class method, polymorphism can’t ever happen. If you then add a non-modified method to the derived class with the same signature as the non-modified base class method, the compiler will generate a Warning message.

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

Share this: