layout | title | parent | nav_order |
---|---|---|---|
default |
Dynamic Binding |
Polymorphism |
6 |
Connecting a method call to the method body is known as binding.
There are two types of binding
1- Static Binding (also known as Early Binding). 2- Dynamic Binding (also known as Late Binding).
Let's understand the type of instance.
Each variable has a type, it may be primitive and non-primitive.
int data=30;
Here data variable is a type of int.
class Dog{
public static void main(String args[]){
Dog d1;//Here d1 is a type of Dog
}
}
An object is an instance of particular java class,but it is also an instance of its superclass.
class Animal{}
class Dog extends Animal{
public static void main(String args[]){
Dog d1=new Dog();
}
}
When type of the object is determined at compiled time(by the compiler), it is known as static binding.
If there is any private, final or static method in a class, there is static binding.
class Dog{
private void eat(){System.out.println("dog is eating...");}
public static void main(String args[]){
Dog d1=new Dog();
d1.eat();
}
}
class Animal{
void eat(){System.out.println("animal is eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("dog is eating...");}
public static void main(String args[]){
Animal a=new Dog();
a.eat();
}
}
Output:dog is eating...