What is Covariance & Contravariance in Java?

Asked 10-Mar-2018
Updated 11-Apr-2023
Viewed 638 times

1 Answer


0

Covariance in Java refers to the ability of a subclass object to be assigned to a variable of its superclass type. It allows a method that returns an object of a subtype to be assigned to a variable of the supertype, which makes the code more flexible and reusable. This is possible because the subclass object contains all the members of the superclass as well as its own additional members, which means that it can be used wherever the superclass object is expected.

Contravariance in Java, on the other hand, is the opposite of covariance. It refers to the relationship between types that allow a supertype to be assigned to a variable of its subtype. This means that a method that takes an object of a supertype as a parameter can be passed a subtype object. Contravariance is useful when dealing with methods that consume objects of a particular type, as it allows more specific subtypes to be passed in without the need for casting.

What is Covariance Contravariance in Java

In summary, covariance and contravariance are concepts that relate to the relationship between types in Java. Covariance allows a subclass object to be assigned to a variable of its superclass type, while contravariance allows a supertype to be assigned to a variable of its subtype. These concepts are essential in object-oriented programming as they enable polymorphism and inheritance, making code more flexible and easier to maintain.