views:

563

answers:

4

Does something like this make any sense at all in Java?

class A<T extends B> extends T{ 
  int fun1() {
    ....
  }      
}

abstract class B extends X implements C {

}

interface C {
  int fun1();
}

I want class B to extend both X and C ideally. But since there is no multiple inheritance in Java, I'm trying to come up with a slicker workaround.

The trick is that the stuff that would be defined in C is defined in Super Class "A" instead. The problem is to get A let me use a generic type after "extends"

Thoughts?

+4  A: 

No, you can't make a type extend a class specified as a type parameter

Without knowing what A, B, C or X are, it's very hard to recommend an alternative design pattern I'm afraid.

Jon Skeet
+1  A: 

What you're trying to do is not going to work - Java generics have nothing to do with inheritance. There is no multiple inheritance, you'll have to make your peace with that :-)

You can declare multiple interfaces and use aggregation to "fake" multiple inheritance.

ChssPly76
A: 

The type parameter is only a placeholder for the compile-time-only generics, it is an actual type.

Trying to come up with a workaround solution for multiple inheritance in a language that does not support it is probably a good indicator that the flaw is in the design. Saying A<T extends B> extends T will obviously fail as it doesn't even mean what you're hoping it to mean - <T extends B> means only that T is of a type that is a B. Appending extends T does not mean anything because T is not defined in that class. Again, this is a compile-time type - the actual type of T is not directly available during runtime.

Are you really sure that what is most properly achieved via multiple inheritance as opposed to composition? It could also be possible that you're trying to get A to do too many things.

A more concrete example might allow others to give more feedback on the design itself.

aberrant80
A: 

As others have said, generics won't help you with multiple inheritance of implementations.

My workaround for not repeating myself due to multiple inheritance is usually to write the common implementation in a static method in the superclass, and delegate to that from the places where it is needed.

The simplest example would be like this:

class A
{
}

class B extends A 
{
}

interface C
{
    void foo();
}

class CBase extends A implements C
{
    public void foo()
    {
        sFoo(this);
    }

    static void sFoo(C c)
    {
        // Implement foo here
    }
}

class D extends B implements C
{
    public void foo()
    {
        CBase.sFoo(this);
    }
}

Note that to make this useful there would be more operations inherited by D from B and A, which are not shown in this example.

starblue