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?
Yes - it's possible (though not with your method signature) and yes, with your signature the types must be the same.
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.
A Generic class can have muliple type parameters.
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.
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.
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.
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.
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.
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.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With