Generic super class in java
Asked Answered
E

3

12

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?

Essonite answered 16/10, 2014 at 8:50 Comment(5)
That would be pretty neat in some cases but I doubt it's possible.Lazaro
Even if this was possible it is useless.Chrissy
@Chrissy Oh, I don't know - it could be like a weak form of multiple inheritance which can at times come in handy. It brings dangers though.Lazaro
@Lazaro How you expect it to work. Since there are no compile time information about methods or fields in LAYOUTTYPE it doesn't add anything to interface.Chrissy
@Chrissy Actually there is some compiletime information: it extends AbstractLayout. I do see many problems with it nevertheless - functions that are defined more than once for example and let's not ignore Type Erasure - but I can also see why someone may want to use something like this at times.Lazaro
R
10

The short answer - no. The type you extends must be an actual type, not a generic type parameter.

Reserved answered 16/10, 2014 at 8:52 Comment(0)
W
3

It sounds like you want to accomplish multiple inheritance, inheriting from both a View and a Layout. This is not possible in Java. You can accomplish something similar with composition. If your GenericView must also provide the functionality given by AbstractLayout, then you can accomplish it like this:

public interface Layout {
    // Layout functions
    public void doLayout();
}

public class GenericView<T extends AbstractLayout> implements Layout {
    private final T delegateLayout;

    // Construct with a Layout
    public GenericView(T delegateLayout) {
        this.delegateLayout = delegateLayout;
    }

    // Delegate Layout functions (Eclipse/IntelliJ can generate these for you):
    public void doLayout() {
        this.delegateLayout.doLayout();
    }

    // Other GenericView methods
}

public class VerticalLayout extends AbstractLayout {
    public void doLayout() {
        // ...
    }
}

After this, you can actually do this:

new GenericView<VerticalLayout> (new VerticalLayout());

Hope this helps.

Watershed answered 16/10, 2014 at 9:8 Comment(0)
L
1

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 of GenericView. 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 have default implementations for many functions in those interfaces (though only if it makes sense of course).

Lazaro answered 16/10, 2014 at 8:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.