KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Is there a way in java to specify, that the type parameter of a generic class must be an interface (not just extending it!) What I want to do is the following: public class MyClass<X extends SomeInterface, Y extends SomeOtherClass & X> Meaning that Y must be a subclass of SomeOtherClass AND implement X. What I currently get by the compiler is The type X is not an interface; it cannot be specified as a bounded parameter So, how can I tell the compiler that X must always be an interface? Edit: OK, I guess I oversimplified my problem a bit. Let's use my actual application domain to make it more clear: I have an API for representing diagrams. A Diagram contains Node and Edge objects. All these three classes implement the Shape interface. Shapes may have child shapes, a parent shape and belong to a diagram. The thing is, that I need to make two versions of this API: one open-source with just basic functionality and an extended one with more features. However, the extended API must only provide methods, which return the extended types ( ExtendedDiagram , ExtendedNode , ExtendedEdge and (here comes the problem) ExtendedShape ). So I have something like this: /* BASIC CLASSES */ public interface Shape<X extends Shape<X,Y>, Y extends Diagram<X,Y>>{ public List<X> getChildShapes(); public X getParent(); public Y getDiagram(); ... } public class Diagram<X extends Shape<X,Y>, Y extends Diagram<X,Y>> implements Shape<X,Y>{...} public class Edge<X extends Shape<X,Y>, Y extends Diagram<X,Y>> implements Shape<X,Y>{...} ... /* EXTENDED CLASSES */ public interface ExtendedShape extends Shape<ExtendedS
Tags (comma-separated)
Save Edits
Cancel