Static and Dynamic binding
- Binding is the process of connecting the function call to the function body
There are 2 types of binding. They are:
- Static
- Dynamic
1. Static Binding
- Type of the object is found at compile time
- If there is a private, static or final method in a class, then static binding takes place
Example
class Example {
private void show() {
System.out.println("Hello world");
}
public static void main(String args[]) {
Example ob = new Example();
ob.show();
}
}
check output here
2. Dynamic Binding
- Type of the object is found at run time
Example
class A {
void show() {
System.out.println("This is class A");
}
}
class B extends A {
void show() {
System.out.println("This is class B");
}
public static void main(String args[]){
A ob = new B();
ob.show();
}
}