Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multilevel Java generic inheritance using extends on generic parameters

Tags:

java

generics

I have

public class First<T> {}

public class Second<T extends SomeConcreteClass> extends First<T> {}

public class Third<T> extends Second<T> {} //Compile-time error

I get the compile-time error

Type argument T is not with bounds of type-variable T.

When I contruct a Third, I want to be able to give the generic parameter as SomeConcreteClass (or derived class thereof), and for a run-time error to be thrown if I've offered up a type that is not part of SomeConcreteClass's inheritance hierarchy.

I would think that the specification in Second's declaration would simply propagate downward, i.e. it should be implicit in the declaration (and any instantiations) of Third.

What's with the error?

like image 560
Engineer Avatar asked Jul 19 '12 12:07

Engineer


People also ask

Can a generic class have multiple generic parameters Java?

Yes - it's possible (though not with your method signature) and yes, with your signature the types must be the same.

How do you extend a generic class in Java?

When generic type invocation happens, the T and E in extends MyGeneric<T, E> would be replaced by type arguments which replaces type parameters T and E in Extend1<T, E> . So, in fact, both generic and non-generic types can extends/implements non-generic types only.

Can a generic class have multiple generic parameters?

A Generic class can have muliple type parameters.

Can generics be used with inheritance in several ways what are they?

Generics also provide type safety (ensuring that an operation is being performed on the right type of data before executing that operation). Hierarchical classifications are allowed by Inheritance. Superclass is a class that is inherited. The subclass is a class that does inherit.

How do you implement multilevel inheritance in Java?

For the implementation of multilevel inheritance, there must be one base class, e.g., A. Then there must be a derived class B which extends class A, and class C extends A.

What is grand parent class in Multilevel inheritance?

It’s pretty clear with the diagram that in Multilevel inheritance there is a concept of grand parent class. If we take the example of this diagram, then class C inherits class B and class B inherits class A which means B is a parent class of C and A is a parent class of B.

What is the difference between linear and multi-level inheritance?

In multilevel inheritance, a parent a class has a maximum of one direct child class only. In multi-level inheritance, the inheritance linkage is formed in a linear way and minimum 3 classes are involved. Code re-usability can be extended with multi-level inheritance.

What are the classes of inheritance in Java?

We have 4 class Person, Staff, TemporaryStaff and MultilevelInheritanceExample (Main class). Here Person class will be inherited by Staff class and Staff class will be again inherited by the TemporaryStaff class.


2 Answers

All you need is

public class Third<T extends SomeConcreteClass> extends Second<T>

You just need to respecify the bound. It doesn't propagate like you think it does.

(I'm not positive of the reason for this, but I have some guesses -- what if it was Third<T> extends Second<Foo<T>>? The appropriate bound on T isn't obvious, if there even is one. So instead, it just doesn't propagate automatically; you have to specify it.)

like image 83
Louis Wasserman Avatar answered Sep 25 '22 01:09

Louis Wasserman


In our case you can probably replace this:

public class Third<T> extends Second<T> {}

with this:

public class Third extends Second<SomeConcreteClass> {}

To use the T within the constructor and otherwise within the code such as a member field of type T, then you can do like this:

VB2 extends VB1<Frag2>
...
public VB2(Frag2 frag){...

VB1<T extends Frag1> extends CommonVB<T>
...
public V1(T frag){... /// notice the T

CommonV<T extends CommonFragBase> extends BaseVB<T>
...
public CommonVB(T controller) {...

BaseVB<T extends CommonFragBase>

That works. The important thing is the generic on the concrete classes had to specify the actual concrete fragment class in order for the other code in the VB class to access its methods.

like image 31
codeslapper Avatar answered Sep 24 '22 01:09

codeslapper