Polymorphism is one of the fundamental concepts in object-oriented programming. It refers to the ability of objects of different classes to respond to the same message (i.e. method call) in different ways, based on the actual class of the object.
There are two main types of polymorphism in OOP: early binding ( also known as static polymorphism or compile-time polymorphism) and late binding (also known as dynamic polymorphism or runtime polymorphism).
1. Early binding (static polymorphism):
In early binding, the method call is resolved at compile-time, based on the declared type of the object reference. This means that the method to be called is determined at compile-time and cannot be changed during runtime. This type of polymorphism is achieved through function overloading and operator overloading.
Here is an example of early binding using function overloading:
2. Late binding( Dynamic polymorphism):
In late binding, the method call is resolved at runtime, based on the actual class of the object. This means that the method to be called is determined during runtime and can change based on the object. This type of polymorphism is achieved through virtual functions.
here is an example of late binding using virtual functions:
In this example, the base class "Shape" has a virtual function "area()", which is overridden in the derived classes "Square" and "Rectangle". The method call to "area()" is resolved at runtime, based on the actual class of the object, through a pointer to the base class. This is an example of late binding.
No comments:
Post a Comment
If you have any doubts, please let me know