In this case it is not a parent reference but an interface reference. Commonly know as programming to an interface. The advantage of doing this is that you don't need to know the implementation details to use the class. You just need to know how the interface is defined. Another advantage is that is you later decide to use a LinkedList instead of an ArrayList then you only have to change the initialization. Otherwise you had to update all your reference types from ArrayList to LinkedList.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter already pointed out the main difference between the 1st and 2nd line, but I can understand your doubts.
I guess one thing you should keep in mind is that most of the time you aren't concerned with the different qualities and features of the different List implementations. So you can just refer to "any implementation of a List" in your code and easily change this implementation as Wouter pointed out.
Of course you are right, if you really need a feature of a particular List implementation you will have to downcast the reference (safely) in order to call specific methods of this implementation. But as I said, most of the time you really don't worry about the implementation details and you should make use of these details wisely and sparingly.
Joined: Mar 23, 2010
Thanks a lot to both of you.
This is when it is an inerface where the implementation details is with the implementer.
But lets consider two classes Parent and Child. where the Child overrides a certain method as well as defines a couple of new methods.
How will then we implement this concept of Parent Reference and the importance of it?
The concept would be the same with two concrete classes. If the child class is a specialized version of the parent class but you are only interested in the base functionality of the parent class the reference type should be that of the parent. This way you could later use a different child implementation with only changing one line of code. If the parent class is abstract it's similar to the example with a parent interface because you simply couldn't instantiate the parent class directly.
Of course you shouldn't overuse this concept of "coding to an interface" although it's usually reasonable to use it. For example just because all classes in Java inherit from class Object it doesn't make sense to make all reference variables of type Object.
The collections framework on the other hand is a typical use case for this concept. Normally you just want a List, a Set or a Map but you don't worry about special methods and features most of the time. Another good reason to code against collection interfaces is that there really are different implementations with different qualities regarding performance for different operations or memory consumption. So it does make sense to make it easy to switch to another implementation later.
As for many patterns or concepts you always have to decide where it's useful and where not. If there are no good reasons that there will ever be different implementations or child classes for a particular class then you shouldn't worry too much about "coding to an interface". Or maybe even better the creator of such a class should emphasize this by making the class final so that it even can't be subclassed.
Joined: Mar 23, 2010
Marco thanks again.
So like my doubts are clear. We use the parent/same reference when we need to put it at the hierarchical level of the parent/same class.
Usage of parent reference helps in hiding the extra implementation defined in its subclasses.
As I wrote above it's usually a good thing to code against an interface/superclass because it makes it easier to maintain and change your code later. But you as a developer still have to decide whether it makes sense or not.
it may be possible that These classes may have their own method which is not declared in List interface.
For Example,If you see for ArrayList ArrayList implements these interfaces.
Cloneable, Collection, List, RandomAccess, Serializable
so there so many method which are not been declared in List interface but are available in ArrayList class.
So you are not able to call those method by using the refernce of List interface.
Shanky, that is exactly what our point is (and not an disadvantage).
We use parent reference when we don't want to use the subclass methods or attributes.
We rather use it for a generic parent class level.
So in this way java prevents this newly created "parent-referred-object" to access the subclass fields.
This is a protection and not a disadvantage.