• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamic binding

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose you have three classes:
First, Second &Third
where,
First is the root class,
class Second extends First &
class Third extends Second.
Now you canreate an object of class Third in the following manner:
Third obj = new Third();
and
Second obj = new Third();
What is the need for going in for the second form of declaration? Also what are the advantages when compared to the first statement? Is this got anything to do with the concept of dynamic binding?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now you canreate an object of class Third in the following manner:
Third obj = new Third();
and
Second obj = new Third();
"What is the need for going in for the second form of declaration? "
when you want to refer to the instance by a reference that restricts its interface to the superclass, while allowing instance method implementations to be the ones in Third. it means your code can deal with Second objects without caring that they might be Thirds.
"Also what are the advantages when compared to the first statement? "
Depends on yr overall application. But generally it means that you code is more flexible because should you one day do this
class AnotherThird extends Second{}
then code that handles Seconds will work with instances of AnotherThird - e.g.

allows this call

whereas if your code refers to implementation reference types

you can't do

because these classes are siblings and there is no automatic conversion.
"Is this got anything to do with the concept of dynamic binding? "
not directly - binding is about the choice of whch method to call. if your classes have only static methods, RTTI/polymorphism is not involved. it is more about architectural flexbility in design, and the ability for code to stay the same when other code changes.
peter
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic