-1

I would like to make a class extend another class among several like this:

class Special extends < Class1<type1> , Class2<Type2>> {
  // ToDo
}

I tried something like:

 class Special<T1, T2, T3, T4> extends < T1<T2> , T3<T4>> {
      // ToDo
    }

Of course that syntax does not compile. How can I do that?


EDIT 1

Let's be clear here: I do NOT want multiple inheritance. I would like my class to extend EITHER one super class, EITHER another one. For that I am asking if there is a possibility doing so using generics.

The same way one can use generics for this special map of array:

class MapArray<T_KEY, T_VALUE> extends LinkedHashMap<T_KEY, ArrayList<T_VALUE>> {

}

This code works an allows the user to put any type for T_KEY and T_VALUE. I would like to know whether is would be possible to use generics with classes.

To put it more simply I would like to do that:

class Special extends < Either_this_class, Either_this_one > {

}
kaligne
  • 3,098
  • 9
  • 34
  • 60
  • A class can only extend one super-class... – TheJavaCoder16 Jul 24 '14 at 15:27
  • possible duplicate of [Can one class extend two classes?](http://stackoverflow.com/questions/6587621/can-one-class-extend-two-classes) – Andrew Stubbs Jul 24 '14 at 15:27
  • Java does not support multiple inheritance; that is, a subclass can only have one parent class. – Barranka Jul 24 '14 at 15:28
  • 2
    This doesn't sound like multiple inheritance. – MxLDevs Jul 24 '14 at 15:30
  • the only thing I can think about is something like: `class Special extends T, T3>` – Barranka Jul 24 '14 at 15:32
  • @Barranka I have read about multiple type parameters when it comes to generics (eg: hashmaps), but what does that do? When I look at it, it looks like incompatible types. – MxLDevs Jul 24 '14 at 15:35
  • @Barranka I'd like to see an example you can compile. – laune Jul 24 '14 at 15:38
  • 1
    Why the negative vote? There is no duplicate here It seems you misunderstod me. I don't want to extend several classes. I would like to my class to extend EITHER one super class, EITHER another. For that I am asking if there is a possibility doing so with using generics. – kaligne Jul 24 '14 at 15:39
  • 1
    This is not possible. A class cannot have a different superclass depending on a type parameter. A class has exactly one superclass and it is always the same, regardless of any other parameter or whatnot that is in scope for the class. – Erwin Bolwidt Jul 24 '14 at 15:50
  • @Erwin Bolwidt SImple and concise comment thanks. COuld you please write it as an answer so that i can mark this thread as solved? – kaligne Jul 24 '14 at 15:52

2 Answers2

1

You can do (staying withing the limits of Java's syntax):

class SubAndImp extends Super implements Interface {
}

or

class ImpAndImp implements InterOne, InterTwo {
}

All or some of the extended and implemented types can be generic:

class SubAndImp<X,Y> extends Super<X> implements Interface<Y> {
}

or

class ImpAndImp<X,Y> implements InterOne<X>, InterTwo<Y> {
}

Your subclass can also stay out of being generic, by instantiating a generic parameter:

class SubAndImp extends Super<Integer> implements Interface<String> {
}
laune
  • 31,114
  • 3
  • 29
  • 42
0

Java supports only multiple interface inheritance. So you cannot extend multiple classes but you can implement any number of interfaces.

Krishna
  • 49
  • 1
  • 2
  • 8