• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extending Generics to Subclasses

 
Brendan Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have a question regarding how to restrict a subclass to only implementing some of the superclass' generics.

GenericOrder can take ComputerPart, Peripheral, Service, Cheese, and Fruit classes. I want the subclass ComputerOrder to only be able to take ComputerPart, Peripheral, and Service classes. Eclipse gives me a warning with the code I've got now so I'm not sure if I have it right or not. This is the Eclipse warning:

Multiple markers at this line
- The type parameter ComputerPart is hiding the type
ComputerPart
- The type parameter Service is hiding the type Service
- The type parameter Peripheral is hiding the type Peripheral



 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you've got the right idea about generics. This line:
doesn't create a class restricted to those 5 types. It declares a class that has 5 generic types, with ComputerPart, Peripheral, Service, Cheese, Fruit just used as the generic placeholders. Here it's not referring to the actual classes ComputerPart etc. You might as well have written:
And following on from that, any class that extends GenericOrder needs to provide classes for all 5 generic types (or be generic itself).

There's no way using generics to have a class operating on 5 specific random classes and no others. That's not what they are for.
 
Brendan Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing that up. Generics are still a bit confusing to me so I'll try and read a bit more about them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic