Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Binding vs Dynamic Binding  RSS feed

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Ernest. Thanks for your reply.
[ September 01, 2008: Message edited by: Arjun Reddy ]
 
Marshal
Posts: 58378
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 58378
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!