• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Classes

 
Sam Samson
Ranch Hand
Posts: 63
IntelliJ IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Why can't I use the super keyword for generic classes?



 
Stephan van Hulst
Bartender
Posts: 6310
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the Java designers thought its usefulness would be very limited, and not worth implementing.
 
Henry Wong
author
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sam Samson
Ranch Hand
Posts: 63
IntelliJ IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6310
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sam Samson
Ranch Hand
Posts: 63
IntelliJ IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6310
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but the question is, why would you want to restrict proper subtypes from being the generic type argument in the first place?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic