aspose file tools*
The moose likes Beginning Java and the fly likes Instance in derived class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instance in derived class" Watch "Instance in derived class" New topic
Author

Instance in derived class

Nick Delauney
Ranch Hand

Joined: Sep 28, 2002
Posts: 43
I am confused by a common situation I see in java. A new Instance is created from a derived class but uses a pointer(or is that reference?) From the superclass.
For Example:
List p = new LinkedList();
why not just code,
LinkedList p = new LinkedList();
another Example:
Set h = new HashSet();
instead of, HashSet h = new HashSet
Please answer in my question with maybe an example of your own or reference my examples please. Thanks
Any help is appreciated.


N.D:"Anything worth having, takes time to get"
Donald R. Cossitt
buckaroo
Ranch Hand

Joined: Jan 31, 2003
Posts: 401
I am familiar with the keyword 'Set' in Visual Basic but have not as yet seen it anywhere in java. (OK, it appears I may have misunderstood your use of 'Set')
A variable of some type object is a reference to that object and not the object itself.
[ April 19, 2003: Message edited by: Donald R. Cossitt ]

doco
Nick Delauney
Ranch Hand

Joined: Sep 28, 2002
Posts: 43
Its interesting because, If I create a List reference, that would mean I am expecting to refer to a List object, however LinkedList is derived and may add to List. So how could a smaller object reference refer to a bigger object ? or more importantly why would you do such a thing ?
[ April 19, 2003: Message edited by: Nick Delauney ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Maybe this will help: Dog's Tale


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

List p = new LinkedList();
why not just code,
LinkedList p = new LinkedList();
another Example:
Set h = new HashSet();
instead of, HashSet h = new HashSet

This is known as the "program to the interface, not to the implementation" rule. The idea is to swap the objects without affecting the code where these objects are used.
subhit chauhan
Ranch Hand

Joined: Jun 20, 2002
Posts: 40
The idea is to swap the objects without affecting the code where these objects are used.
agree with Eugene
List, Set are interfaces and not the classes...
You cannot instantiate them....
you can create directly the LinkedList object or the HashSet object also.... these are the examples of OOPS... the basic three principle of OOPS encapsulation, inheritance and polymorphism
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I think what everyone is trying to say is that using List or Set makes it easy to change from a LinkedList to some other List implementation. In such a case, the less code you have to change, the less chance you have of introducing defects.


Java API Documentation
The Java Tutorial
aadhi agathi
Ranch Hand

Joined: Apr 29, 2002
Posts: 263
Originally posted by Layne Lund:
I think what everyone is trying to say is that using List or Set makes it easy to change from a LinkedList to some other List implementation. In such a case, the less code you have to change, the less chance you have of introducing defects.

that's one thing and the other one is about exposing things to the client. The client can depend on 1)the return value of Your class's method or 2)the instance variable of Your class.(not prescribed).
3)the parameter type of Your class's method
4)Your class's interface to achieve polymorphism.
The less exposure will stop the "ripple effect" into the client code. Means, that if Your class is having a method f1 , returning List then
client will code
List l = new YourClass().f1();
But YourClass's f1 can use any implementation , for example ArrayList, LinkedList , SortedList ot whatever ...
but, if f1 is returning LinkedList then
client will code
ArrayList l = new YourClass().f1();
when you feel ArrayList is not effecient ,then the client will also need to get change .
In simple code to Spouse and not to Wife/Husband.
Spouse->interface
Wife/Husband-> implementation
Please also note the abstraction usage here . unless the Husband want to code to a Wife like
then, it's better to use the higher abstraction of Spouse.
Moral of the story:
go higher the abstraction to achive better polymorphism/substitutability
Hope i am clear


Aadhi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by aadhi agathi:
The less exposure will stop the "ripple effect" into the client code. Means, that if Your class is having a method f1 , returning List then ... (snip)

Well, I wasn't planning on going down that road since the original question only dealt with creating an object. However, the point is well taken that this kind of programming is even more useful with return types and parameters of a method, as illustrated.
[ April 23, 2003: Message edited by: Layne Lund ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instance in derived class