File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What is the use of super word here? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What is the use of super word here?" Watch "What is the use of super word here?" New topic
Author

What is the use of super word here?

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
I cannot understand the use of 'super' keyword in this case.

Suppose I have dog extending Animal and the following code:


This doesn't let me add its superclass Animal.
So my problem is, what is the use of 'super'.
what wouldn't I just use dog without super?

But when I do this:


It compiles fine, BUT still doesn't let me add an Animal()?
Intuitively Animals (superclass) shouldnt be allowed to get added in to dog reference List, but my problem is why use super in this case.
what gives?

what is the reason that I have a dog type reference and an Animal type List, but I can ONLY add dogs?

Any help is appreciated.
Thank you.

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2584
    
    9

take a look java Tutorial Generics

the "more fun with wildcards" discuss about super word


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Mumtaz Khan
Ranch Hand

Joined: Feb 21, 2008
Posts: 53
When you use the <? super Dog> syntax, you are telling the compiler you can accept the type on the right hand side of super or any of its supertype.
Thus a collection declared as any supertype of Dog will be able to refer a collection of Dog's supertype,(Animal) or Object.
However while adding an element to the collection, you can only add the type on the right hand side of super.., i.e Dog. Well a Dog is an animal but an Animal is not a Dog so you can refer to a Collection of type Animal but can not add an animal.

SCJP 5.0 -- 97%
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thank you very much.

So I can define the reference with the <? super> so that the actual collection type (right hand side) can be left hand side or its super type.. but when I add, I can only add whatever I defined on the right hand side.

but any particular use of this?
I know the use super in a method argument, so I can 'add' any supertype of the reference type safely, but why define a reference in this way?
If for example, I say,


But I can only add Animal objects into this, and I even cannot safely call Dog methods on the items inside the dog list.
SO what purpose does this serve?
Isn't this risky? because at compile time, dog list can be a list of Dogs or Animals.
But I cannot invoke Dog methods on the items inside dog list. Because the items can be Dogs or Animals?

What am I missing here.


Thank you.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Check the link to the generics tutorial. A rule of thumb is that if you use the ? wildcard, you cannot alter a collection in any way. You may still read from a collection for example with an iterator, but you cannot add or remove items. Generics are stripped at runtime, so while the compiler ensures a type safe collection, the JVM cannot be sure of what you add or remove from the collection if a wildcard is used, so you cant add / remove. Its tricky, but after a while and a bit of reading you will grasp it.


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thank you Stephen.
Having a read now..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the use of super word here?