| Author |
Static Binding vs Dynamic Binding
|
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 624
|
|
Can someone please tell me if am wrong. Dynamic binding is runtime polymorphism and static binding is compile time polymorphism right? Dynamic binding example would be overriding (Methods are invoked based on object of a class) Static binding example would be overloading (Methods are invoked based on reference type of a class) Am I correct guys? Thanks.
|
Be Humble... Be Nice.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
"Compile time polymorphism" is another word I hate. Resolving overloaded methods is not polymorphism. Method signatures include their argument types, and so overloaded methods are distinguished at compile time just as if they had different names. "Compile-time polymorphism" makes it sound as if something more is happening, but it's not. Please don't use this term. Your examples of "dynamic binding" and "static binding" are perfect, though.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 624
|
|
Ok Ernest. Thanks for your reply. [ September 01, 2008: Message edited by: Arjun Reddy ]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32838
|
|
Sort of, . . . y-e-e-e-s . . . well, mostly. It sounds right, but not precise. Dynamic binding means the runtime finds the type of an object (probably from its Class<T> object) and uses that type to look for the types of methods invoked. As you imply, that applies to overridden class members. And the only kind of class member you can override is an instance method. Static binding means the compiler finds the type of an object from the declaration of its reference and uses that type to look for the types of members. That applies to non-overridden class members, ie static fields and methods. I am not sure whether it applies to instance fields; can't remember offhand, but you oughtn't make non-constant fields visible outside their class in the first place. I don't think any of this applies to overloading.
|
 |
prathamesh Junnarkar
Greenhorn
Joined: Feb 23, 2010
Posts: 1
|
|
If there are classes as follows
Class A{
int num = 10;
int get(){
return num;
}
}
Class B extends A
{
int num = 20;
int get(){
return num;
}
}
A a = new A();
A a1 = new B();
Static Binding: Static / Early binding is compile time binding. It means at compile time jvm decides which class member or method is preferred to call.
e.g.
a.num will be 10 and
a1.num will be 10
Dynamic binding : Late binding si run time binding. At run time jvm decides which method to call, depending on object type. In this case
a.get() will return 10
a1.get() will return 20
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32838
|
|
Welcome to the Ranch
|
 |
 |
|
|
subject: Static Binding vs Dynamic Binding
|
|
|