Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend from Generic Supertype?

Tags:

In Java, am I able to extend from a generic supertype? According to this article, it looks like I should be able to: http://www.ibm.com/developerworks/java/library/j-djc05133.html.

public abstract class MyClass<T> extends T { 

However, when I do something similar in my application, I get the following error: "Cannot refer to the type parameter T as a supertype."

Does anyone know if I am able to extend from a generic supertype in Java? And, if so, is there something special needed to make this happen?

EDIT: I read the article wrong. It's actually discussing the potential problems of this.

like image 735
Stephen Watkins Avatar asked Jan 04 '10 16:01

Stephen Watkins


People also ask

Can we extend generic class in Java?

Yes, you can use any valid Java identifier for type parameters.

What is the difference between List <? Super T and List <? Extends T?

extends Number> represents a list of Number or its sub-types such as Integer and Double. Lower Bounded Wildcards: List<? super Integer> represents a list of Integer or its super-types Number and Object.

What does <? Extends mean in Java?

The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.

What does <? Super t mean in Java?

super T denotes an unknown type that is a supertype of T (or T itself; remember that the supertype relation is reflexive). It is the dual of the bounded wildcards we've been using, where we use ? extends T to denote an unknown type that is a subtype of T .


2 Answers

You are able to inherit from a supertype that has generic type parameters. For instance:

public class MyList<T> extends AbstractList<T> 

(or even give the parameters bounds, e.g.):

public class MyStringList extends AbstractList<String> 

You are not able to define a class that has a wildcard supertype. Such a concept doesn't make any sense in Java (and I'm not convinced it has much value in the abstract). In your MonitoredDevice example, I'm not even sure what functionality you expect there.

Because of erasure this is simply not possible at a fundamental level of the way Java works. Each class must have a superclass - what is the superclass of MonitoredDevice? What methods are available on a MonitoredDevice object? You wouldn't be able to call any inherited methods on this class as the compiler could not guarantee such methods exist, nor could the bytecode represent such calls.

I suspect that what you are trying to do could be better achieved with dynamic proxies.

Edit: OK, after reading the article fully I can see what the motivation behind such a declaration would be. But after reading the article fully, you also realise that this isn't supported by Java at present. The fundamental reason why is due to erasure; such a technique would never be possible under the current mechanics, and the article goes into a lot more detail of what specifically prevents this from working.

like image 52
Andrzej Doyle Avatar answered Sep 20 '22 07:09

Andrzej Doyle


From that error message it sounds like you're trying to extend from the type parameter itself, which you cannot do because the type is erased at runtime. If this isn't what you're trying to do, please post your code.

like image 36
danben Avatar answered Sep 20 '22 07:09

danben