---
title: "What is Covariance & Contravariance in Java?"  
description: "What is Covariance & Contravariance in Java?"  
author: "Arti Mishra"  
published: 2018-03-10  
updated: 2023-04-11  
canonical: https://answers.mindstick.com/qa/38316/what-is-covariance-contravariance-in-java  
category: "programming"  
tags: ["java"]  
reading_time: 2 minutes  

---

# What is Covariance & Contravariance in Java?

## Answers

### Answer by Mark John

**Covariance in [Java](https://www.mindstick.com/articles/1702/introduction-to-java)** refers to the ability of a subclass object to be assigned to a [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) of its superclass type. It allows a method that [returns an object](https://www.mindstick.com/blog/383/javascript-string-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](https://www.mindstick.com/blog/11087/inheritance-in-java-instantiating-a-subclass) 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](https://www.mindstick.com/articles/54917/how-to-keep-her-happy-in-a-long-distance-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](https://www.mindstick.com/articles/12162/overloading-and-overriding-in-php) 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?](https://answers.mindstick.com/questionanswer/5284446b-7b20-42b5-901f-b99dc26514d3/images/90e2a128-a963-4b94-9e12-cb23c0b237ba.png)

In summary, **covariance and contravariance** are [concepts](https://www.mindstick.com/interview/490/explain-the-concepts-and-capabilities-of-assembly-in-dot-net) 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](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) as they enable [polymorphism](https://www.mindstick.com/articles/32/polymorphism) and [inheritance](https://www.mindstick.com/articles/80/inheritance-in-c-sharp), making code more flexible and easier to maintain.


---

Original Source: https://answers.mindstick.com/qa/38316/what-is-covariance-contravariance-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
