OOPS in JAVA 17
Static Binding and Dynamic Binding
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).
We can perform polymorphism in java by method overloading and method overriding.
static binding
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.
Example
class Dog{
private void eat(){System.out.println("dog is eating...");}
public static void main(String args[]){
Dog d1=new Dog();
d1.eat();
}
}
Output: dog is eating...
Dynamic binding
WhenWhen type of the object is determined at run-time, it is known as dynamic binding.
Example
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...
No comments:
Post a Comment
Hai , Post your comment . (required, Bugs, Errors )