wood burning stoves 2.0*
The moose likes Java in General and the fly likes Use of Parent reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Use of Parent reference" Watch "Use of Parent reference" New topic
Author

Use of Parent reference

tarun saha
Greenhorn

Joined: Mar 23, 2010
Posts: 27
What is the difference these two lines.

List listObject = new ArrayList(); // Parent reference and child instance
ArrayList arrayList = new ArrayList(); // Direct same class reference

What is reason to use the first line.
I have used both but could not spot a difference.

Instead if I use the first line in other such examples of polymorphism, I am not able to use the new functionality of the child class(unless downcasted).

So my main question is why do we use the "Parent Reference" at all?
Just to main it generic from that hierarchy of inheritence? so that it can be applicable for all other subclasses of it?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

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.

Marco
tarun saha
Greenhorn

Joined: Mar 23, 2010
Posts: 27
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?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

You're welcome

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.

Marco
tarun saha
Greenhorn

Joined: Mar 23, 2010
Posts: 27
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.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Exactly

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.

Marco
tarun saha
Greenhorn

Joined: Mar 23, 2010
Posts: 27
Yup we are on the same page now
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

One disadvantage of the first line is that(may be someone has already point out)

As list is a interface and the classes which implements this interface is
AbstractList, ArrayList, LinkedList, Vector

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.

to see the method of ArrayList class have a Look at here


SCJP6.0,My blog Ranchers from Delhi
tarun saha
Greenhorn

Joined: Mar 23, 2010
Posts: 27
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.

Am I able to clear your doubt?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of Parent reference
 
Similar Threads
Doubts about Generics
difference in instances
difference in the instance
Java processes
java basics