Early binding means calling a non-virtual method that is decided at a compile
time while Late binding refers to calling a virtual method that is decided at a
runtime.
The polymorphism is achieved by early binding and late binding
Early Binding:
Compiler bind the objects to methods at the compile time.This is called early binding or static binding.Function overloading is example for early binding.
Late Binding:
Compiler bind the objects to methods at the runtime.This is called late binding or dynamic binding.Function overriding is example for late binding.
What is Dynamic Binding and static binding ?
Ans: Well Dynamic binding is objects are binded to the reference object on runtime where as static binding is the binding in which the compiler is straight away aware of which object is binded before runtime.
dynamic binding is also called late binding.
I have shown an example below which uses two classes DynamicBinding and DBInherit which inherits the DynamicBinding and overrides an method of the above class , so what happens if create a base class reference and initialise it with junior class object , then I will get the junior class method and not the base class method as it is overridden and the compiler knows it only at runtime.
Hence the output will show the junior class method output (DBInherit is the junior class referred here) and not the base class.
DynamicBinding.cs class source code:
Program.cs class source code:
The output will look like as shown below :
The polymorphism is achieved by early binding and late binding
Early Binding:
Compiler bind the objects to methods at the compile time.This is called early binding or static binding.Function overloading is example for early binding.
Late Binding:
Compiler bind the objects to methods at the runtime.This is called late binding or dynamic binding.Function overriding is example for late binding.
What is Dynamic Binding and static binding ?
Ans: Well Dynamic binding is objects are binded to the reference object on runtime where as static binding is the binding in which the compiler is straight away aware of which object is binded before runtime.
dynamic binding is also called late binding.
I have shown an example below which uses two classes DynamicBinding and DBInherit which inherits the DynamicBinding and overrides an method of the above class , so what happens if create a base class reference and initialise it with junior class object , then I will get the junior class method and not the base class method as it is overridden and the compiler knows it only at runtime.
Hence the output will show the junior class method output (DBInherit is the junior class referred here) and not the base class.
DynamicBinding.cs class source code:
Program.cs class source code:
The output will look like as shown below :
No comments:
Post a Comment