Inheritance Rules for Generic Types

Java-Inheritance Rules for Generic Types

Inheritance Rules for Generic Types

When you work with generic classes, you need to learn a few rules about inheritance and subtypes. Consider the following classes and statements :

Inheritance Rules for Generic Types

The class ComputerStudent is a subclass of class Student. Is MyClass<ComputerStudent> a subclass of MyClass<Student>? Perhaps surprisingly, the answer is “no.” The third statement will not compile and it is illegal to assign one to the other. In general, there is no relationship between MyClass<TypeT> and MyClass<TypeV>:

Inheritance Rules for Generic Types

This seems like a cruel restriction, but it is necessary for type safety. Suppose we were allowed to convert a MyClass<ComputerStudent> to a MyClass<Student>. Consider the following code, the second line is illegal, but suppose it wasn’t :

Inheritance Rules for Generic Types

Now mycs and mys refer to the same object. The setT() method of mys try to set the Student object, this can cause error, because Student cannot be converted to ComputerStudent.

Consider the following classes and statements :

Inheritance Rules for Generic Types

The class Derived that extends the class Base, both Base and Derived are generic classes. They require type parameters to instantiate them where they are used. Notice the second statement, the statement that will not compile because the type parameters are not exactly the same and that there is no inheritance relationship among parameter types in generics. The rule is that for the simple types(concrete type instantiations) of generic instantiations, inheritance applies only to the “base” generic type and not to the parameter types. Furthermore, assignability applies only when the two generic types are instantiated on exactly the same parameter type. It is possible for an instantiation of Derived to be an instantiation of Base, but only if the parameter types are exactly the same. Inheritance doesn’t follow the parameter types:

Inheritance Rules for Generic Types

This is necessary for type safety. Suppose we assign a Derived<ComputerStudent> to a Base<Student>. Consider the following code, the second line is illegal, but suppose it wasn’t :

Inheritance Rules for Generic Types

Now dri and bas refer to the same object. The setT() method of bas try to set the Student object, this can cause error, because Student cannot be converted to ComputerStudent.

Program

Inheritance Rules for Generic Types


Program Source

class Student {}

class ComputerStudent extends Student {}

class Base<T> {
    
    T t1;
    
    void setT(T t){
        
        t1=t;
    }
}

class Derived<T> extends Base<T> {
    
    void setT(T t){
        
        t1=t;
    }
}

public class Javaapp {

    public static void main(String[] args) {
        
        Derived<ComputerStudent> dri = new Derived<ComputerStudent>();
        Base<Student> bas = dri;
        bas.setT(new Student());
    }
}

Leave a Comment