0% found this document useful (0 votes)
212 views

Early Binding and Late Binding

Early binding occurs at compile time, where the method to be called is fixed. Late binding occurs at runtime, where the method is determined based on the actual object type. The examples show early binding using a base class pointer calling a base class method, while late binding uses virtual functions and overrides the base class method when using a derived class object.

Uploaded by

Ali Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
212 views

Early Binding and Late Binding

Early binding occurs at compile time, where the method to be called is fixed. Late binding occurs at runtime, where the method is determined based on the actual object type. The examples show early binding using a base class pointer calling a base class method, while late binding uses virtual functions and overrides the base class method when using a derived class object.

Uploaded by

Ali Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Early Binding:Early Binding Always Occur in the Polymorphism, when we pass the Reference

of a sub Class into the Pointer Object of Base Class, then the Member Functions Are never to
be Override. When we execute the Program then Compiler knows this thing. This is called as
Early Binding. And the Compiler will Execute the Member Functions of Base Class and this will
never overrides the Body of the Sub Class Member Function. This is known as the early
binding.
Example

#include<iostream>

using namespace std;

class Base {

   public:

   void display() {

      cout<<" In Base class" <<endl;

   }

};

class Derived: public Base {

   public:

   void display() {

      cout<<"In Derived class" << endl;

   }

};

int main(void) {

   Base *base_pointer = new Derived;

   base_pointer->display();

   return 0;

Late Binding: In the Late Binding the Compiler never knows About the Code. Means what the
Code will do. All the Code is understood at the Time of Execution This is called as Late Binding.
As we Know that Late Binding is Performed By using the virtual Functions. Virtual Means
Function must be Override. When the Compiler Finds out the Word Virtual at the time of
Execution then this will override the Body of the Function of Base Class with the Function of sub
Class. When we pass the Reference of the Sub Class into the Pointer Object of the Base Class
then the Body of the First Function will be Override by the Sub Class Because the Base Class
Member Function is declared with the virtual keyword and the virtual means function must be
override.
Example

#include<iostream>

using namespace std;

class Base {

   public:

   virtual void display() {

      cout<<"In Base class" << endl;

   }

};

class Derived: public Base {

   public:

   void display() {

      cout<<"In Derived class" <<endl;

   }

};

int main() {

   Base *base_pointer = new Derived;

   base_pointer->display();

   return 0;

Summary:

The Early Binding just means that the target method is found at compile
time while in Late Binding the target method is looked up at run time.
Most script languages use late binding, and compiled languages use
early binding.

Difference between method overloading and method overriding

Method overloading happens in the same class shares the same method name
but each method should have different number of parameters or parameters
having different types and order. But in method overriding derived class have the
same method with same name and exactly the same number and type of
parameters and same return type as a parent class.

Method Overloading happens at compile time while Overriding happens at


runtime. In method overloading, method call to its definition has happened at
compile time while in method overriding, method call to its definition happens at
runtime.

In method Overloading, two or more methods shares the same name in the same
class but having different signature while in method overriding, method of parent
class is re-defined in the inherited class having same signature.

In the case of performance, method overloading gives better performance when


compared to overriding because the binding of overridden methods is being done
at runtime.

Static binding is happening when method overloaded while dynamic binding


happens when method overriding.

Method overloading add or extend more to the method functionality while


method overloading is to change the existing functionality of the method

Static methods can be overloaded, that means a class can have more than one
static method of same name. But static methods cannot be overridden, even if
you declare a same static method in derived class, it has nothing to do with the
same method of base class.

You might also like