| Author |
Generic Classes
|
Sam Samson
Ranch Hand
Joined: Oct 08, 2011
Posts: 60
|
|
Hi
Why can't I use the super keyword for generic classes?
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3047
|
|
|
Because the Java designers thought its usefulness would be very limited, and not worth implementing.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
Sam Samson wrote:
Why can't I use the super keyword for generic classes?
To elaborate more on Stephan's response, the Object class is a super class of Zoo, hence, if supported, this generic class can support any Object type, which isn't much different than simply GenericTest<T>.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4747
|
|
Henry Wong wrote:To elaborate more on Stephan's response, the Object class is a super class of Zoo, hence, if supported, this generic class can support any Object type, which isn't much different than simply GenericTest<T>.
Not much, but it is different (at least as I understand it). For example, I'm pretty sure that
? super Integer
allows the substitution of Number or Object, but not String, so I suppose there might be a use for Sam's suggestion.
As you and Stephan both say, probably not enough of one to warrant implementing though.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Sam Samson
Ranch Hand
Joined: Oct 08, 2011
Posts: 60
|
|
To elaborate more on Stephan's response, the Object class is a super class of Zoo, hence, if supported, this generic class can support any Object type, which isn't much different than simply GenericTest<T>.
Henry
The same thing I could say about:
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4747
|
|
Sam Samson wrote:The same thing I could say about:
Ah, but that's different. There's absolutely no difference between what you put and
public class GenericClassTest<T>
Winston
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3047
|
|
Well, if you have a GenericTest<T super Zoo>, and Zoo extends Object, then the only valid types are GenericTest<Object> and GenericTest<Zoo>. You wouldn't be allowed to define any GenericTest<X>, where X is a proper subtype of Zoo.
This almost eliminates the entire point of generics. The point is that generics allow you to let methods have a more specific return type than what is known at the time of writing the method. A lower bound restricts this ability.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4747
|
|
Stephan van Hulst wrote:This almost eliminates the entire point of generics. The point is that generics allow you to let methods have a more specific return type than what is known at the time of writing the method. A lower bound restricts this ability.
I totally agree. I was just trying to point out that the 'super' keyword is not a blanket mandate to substitute any object type.
Winston
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Winston Gutkowski wrote:
Henry Wong wrote:To elaborate more on Stephan's response, the Object class is a super class of Zoo, hence, if supported, this generic class can support any Object type, which isn't much different than simply GenericTest<T>.
Not much, but it is different (at least as I understand it). For example, I'm pretty sure that
? super Integer
allows the substitution of Number or Object, but not String, so I suppose there might be a use for Sam's suggestion.
Why would it need generics anyway? Just put Integer anywhere you would put T and you get just about the same thing.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Sam Samson
Ranch Hand
Joined: Oct 08, 2011
Posts: 60
|
|
Winston Gutkowski wrote:
Not much, but it is different (at least as I understand it). For example, I'm pretty sure that
? super Integer
allows the substitution of Number or Object, but not String, so I suppose there might be a use for Sam's suggestion.
That's what I meant. Why it's allowed downwards the inheritance-tree but not upwards?
Rob Spoor wrote:
Why would it need generics anyway? Just put Integer anywhere you would put T and you get just about the same thing.
But what about polymorphism? For example Class A, Class B extends A, Class C extends B etc.
If I want to allow only superclasses of B I only can write <? extends A>, but this also allows to pass in a C, right? With <? super B> it would only be possible to pass in an A or a B, no C. So I have higher restriction. If I'm wrong, please correct me.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3047
|
|
|
Yes, but the question is, why would you want to restrict proper subtypes from being the generic type argument in the first place?
|
 |
 |
|
|
subject: Generic Classes
|
|
|