AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-OOPs: What is the difference between static binding and dynamic binding?

Please explain point to point difference between static and dynamic binding with sample source code.

corejava x 353
OOPs x 49
Posted On : 2013-11-24 19:18:42.0
profile Rishi Kumar - anyforum.in Rishi Kumar
523188245674
up-rate
4
down-rate

Answers


Connecting a method call to the method body is known as binding.
There are two types of binding:
static binding (also known as early binding).
dynamic binding (also known as late binding).

*********************************** static binding *************************************

When type of the object is determined at compile 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 of 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();
}
}


*********************************** dynamic binding ************************************

When type of the object is determined at run-time, it is known as dynamic binding.

------------------------------------- Example of dynamic binding ----------------------------------------

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...
------------------------------------------------------------------------------------------------------------------

In the above example object type cannot be determined by the compiler, because the instance of Dog is also an instance of Animal.So compiler doesn´t know its type, only its base type.

Posted On : 2013-11-24 19:29:09
Satisfied : 1 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129554551
Reply This Thread
up-rate
4
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in