posted 20 years ago
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