I have the following code:
public class Inheritance {
class A<T,U,V>{
}
class B<T,U,V> extends A<T,U,T>{
}
}
Could somebody explain me, how it actually works? Does the Class B extend only A class, which parameters are "T,U,T", or it extends the actual A"T,U,V" class?
Let put it into real example:
public class Inheritance {
public static class A<T,U,V>{
T t;
U u;
V v;
A(T t, U u, V v) {
this.t = t;
this.u = u;
this.v = v;
}
T getT() {return t;}
U getU() {return u;}
V getV() {return v;}
}
public static class B<T,U,V> extends A<T,U,T>{
public B(T t, U u, V v) {
super(t, u ,t);
}
}
public static void main(String[] args) {
B<Boolean, Integer, String> b = new B<>(false, 1, "string");
// 't' attribute is Boolean
// since type parameter T of class B is Boolean
Boolean t = b.getT();
// 'v' attribute is Boolean
// since type parameters T and V of class A must have the same type as
// type parameter T of class B
Boolean v = b.getV();
}
}
Basically class B extends class A (which has three generic params). By declaring B<T,U,V> extends A<T,U,T>
you just bind the A's first and A's third generic param to the same type of B's first param
As shown in example in constructor of class B we have three distinct types - Boolean, Integer, String, but in constructor of class A we have only two distinct types Boolean, Integer because 1st and 3th constructor param of class A are both bound to Boolean type
More on generics and inheritence can be found here: https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html