Instantiation Question

R

Rhino

Can someone answer a question that has been bothering me sporadically for a
while? I'm afraid I'm weak in OO theory and don't know where to look this
up.

What is the difference between:

TreeSet set = new TreeSet();

and

Set set = new TreeSet();

I'm not particularly concerned with Sets or TreeSets, this is just an
example of an idiom I see occasionally in examples. The same issue occurs in
the examples:
GregorianCalendar cal = new GregorianCalendar();
Calendar cal = new GregorianCalendar();

The first one of each pair makes sense to me: I'm making an instance of a
specific class via a "new". But the second of each pair mystifies me: what
is the difference between these two and what are the implications of each
approach? In other words, what are the consequences of specifying a subclass
of the original class after the new keyword versus specifying the original
class name after the new keyword?
 
C

Christophe Vanfleteren

Rhino said:
Can someone answer a question that has been bothering me sporadically for a
while? I'm afraid I'm weak in OO theory and don't know where to look this
up.

What is the difference between:

TreeSet set = new TreeSet();

and

Set set = new TreeSet();

I'm not particularly concerned with Sets or TreeSets, this is just an
example of an idiom I see occasionally in examples. The same issue occurs
in the examples:
GregorianCalendar cal = new GregorianCalendar();
Calendar cal = new GregorianCalendar();

The first one of each pair makes sense to me: I'm making an instance of a
specific class via a "new". But the second of each pair mystifies me: what
is the difference between these two and what are the implications of each
approach? In other words, what are the consequences of specifying a
subclass of the original class after the new keyword versus specifying the
original class name after the new keyword?

First of all, those are not subclasses, those are implementations of the
interface.

eg:
List list = new ArrayList();

Here List is the interface, and ArrayList is a class that implements that
interface.

The advantage of this is that you're not tying yourself to a specific
implementation, which makes it much easier to change your code to another
type of List if that should be necessary.

LinkedList for example, because of its specific implementation, is much
faster than ArrayList for certain operations, like deleting an item
somewhere in the middle, ...

So for those cases where you need fast insertion (not at the end of the list)
you'd use List list = new LinkedList(), and all your code would still work.
But if you need to be able to retrieve the nth element of a List, an
ArrayList is much faster.

You can find more info about that at
http://developer.java.sun.com/developer/JDCTechTips/2002/tt0910.html

The same principle stands for all other cases where you can use an interface
instead of the actual implementation, it's just more flexible.
 
D

David Robbins

Rhino said:
Can someone answer a question that has been bothering me sporadically for a
while? I'm afraid I'm weak in OO theory and don't know where to look this
up.

What is the difference between:

TreeSet set = new TreeSet();

in this case the variable 'set' is defined as the type 'TreeSet' so all the
functionality of the TreeSet class is available to it.
and

Set set = new TreeSet();

in this case 'set' is defined as the type 'Set', which should normally be a
parent of 'TreeSet' or an interface that 'TreeSet' impliments. When used
this way only the functionality defined for 'Set' is available, any
extensions added by 'TreeSet' would not be accessible without casting set
back to a TreeSet type which is not generally recommended. The advantage
here is that you could also do things like:
Set set=new BushSet();
Set set=new ForestSet();
and you could use the variable 'set' without knowing really what type of set
it was implimenting. This is useful when you only need some of the
information about each of the types of sets (the part defined in 'Set') and
don't care about the specifics of the individual classes.
I'm not particularly concerned with Sets or TreeSets, this is just an
example of an idiom I see occasionally in examples. The same issue occurs in
the examples:
GregorianCalendar cal = new GregorianCalendar();

when doing this you restrict 'cal' to be a specific type of calendar. In
this case you could only use functions and properties that were defined in
the GregorianCalendar class.
Calendar cal = new GregorianCalendar();

in this case you could have the user select the type of calendar, say in one
spot the user picked Gregorian you could define it as above. then somewhere
else they wanted:
Calendar cal = new ChineseCalendar();
or
Calendar cal = new LunarCalendar();
as long as ChineseCalendar and LunarCalendar are children of or implement
the Calendar interface you could use 'cal' interchangeably and make calls
like
cal.getDay()
cal.getMonth()
etc, and not care which type of calendar was actually being used. (as long
as Calendar defined getDay and getMonth of course).
but you could not assume it was a ChineseCalendar and call something like:
cal.getYearOfTheDragon()
that was unique to the ChineseCalendar class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,907
Messages
2,570,008
Members
46,365
Latest member
Irvin51514

Latest Threads

Top