Is it possible to create something like this in java
public abstract class GenericView<LAYOUTTYPE extends AbstractLayout> extends LAYOUTTYPE
so that
public class MyView extends GenericView<HorizontalLayout>
extends GenericView
and HorizontalLayout
and
public class MyView2 extends GenericView<VerticalLayout>
extends GenericView
and VerticalLayout
?
It sounds like you want to accomplish multiple inheritance, inheriting from both a
View
and aLayout
. This is not possible in Java. You can accomplish something similar with composition. If yourGenericView
must also provide the functionality given byAbstractLayout
, then you can accomplish it like this:After this, you can actually do this:
Hope this helps.
Sadly this is not possible in Java. The main reason I can think of is the problem with Type Erasure - once that class is compiled it will no longer know what
LAYOUTTYPE
is.What I think you're trying to achieve is a sort of multiple inheritance - so you can combine features from
LAYOUTTYPE
with those ofGenericView
. Multiple inheritance is - as you probably know - not possible in Java. However you can use multiple interfaces which for many cases will be sufficient. If you're using Java 8 you can even havedefault
implementations for many functions in those interfaces (though only if it makes sense of course).The short answer - no. The type you
extends
must be an actual type, not a generic type parameter.