This blog is about providing theory as well as simple executable codes of different programming languages such as java, C, C++, and web programming, etc. This blog will be helpful to the IT students to learn about programming.

Wednesday, February 15, 2023

what is polymorphism? explain early binding and late binding concept with program

 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:

#include<iostream>
using namespace std;

class Shape{
    public:
        void area(int side){
            cout<<"area of square:"<<side*side<<endl;
        }
        
        void area(int length, int breadth){
            cout<<"area of rectangle:"<<length*breadth<<endl;
        }
};
int main(){
    Shape shape;
    shape.area(3); //early binding
    shape.area(6,9);//early binding
    return 0;
}

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:

#include <iostream>
using namespace std;

class Shape {
    public:
        virtual void area() {
           cout << "Area of Shape" << endl;
      }
};

class Square : public Shape {
    public:
         void area() {
              cout << "Area of Square" << endl;
          }
};

class Rectangle : public Shape {
    public:
         void area() {
              cout << "Area of Rectangle" << endl;
          }
};

int main() {
  Shape *shape;
  Square square;
  Rectangle rectangle;
  shape = &square;
  shape->area(); // late binding
  shape = &rectangle;
  shape->area(); // late binding
  return 0;
}

 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

Slider Widget