File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Dynamic binding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Dynamic binding " Watch "Dynamic binding " New topic
Author

Dynamic binding

Jacob Thomas
Ranch Hand

Joined: May 06, 2003
Posts: 51
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?
peter greaves
Ranch Hand

Joined: Sep 27, 2002
Posts: 51
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


SJCP 1.2
 
wood burning stoves
 
subject: Dynamic binding
 
Similar Threads
Doubt in Object Reference
instanceof
Reason for not having dynmaic binding for instance variables
operator instanceof confusion
shadowing variables