Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 24220
40
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: 60127
188
  • 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: 60127
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!