Variance is the interconnection of Sub-Typing relationships which are either of complicated types or of their constituent types. Scala provides three types of variance:
Covariance states that if there are two parameterized types such that S is a subtype of T, then List[S] is a subtype of List[T]. This is an inheritance relationship. So, this basically defines the relationship whether we can replace a type by its base type. Simply putting in words, if we take an example where Car is a subtype of Vehicle, then List[Car] is a subtype of List[Vehicle]. Therefore, we can replace List[Car] by List[Vehicle]. When we declare a type to be covariant, then its safe use at various positions becomes limited. In the case of immutable types, covariance is widely used.
Here, T is a type parameter and the “+” symbol represents Scala covariance.
Let us discuss this concept with the help of examples:
White Lily Jersey Lily White carnations Pink carnations
Explanation: In the above example, Lily and Carnation are subtypes of Flower. So, it is evident that a List[Lily] is a List[Flower] and a List[Carnation] is also a List[Flower], and we can substitute any of them for a List[Flower]. In the later part of the code, there is a method FlowerNames that prints names of flowers and the acceptable argument is a list of flowers. If the two lists are covariant, only then the method calls will compile and the flower names will get printed respectively. So, as Lily and Carnation are a subtype of Flowers and the last two lines will execute due to covariance.
- Abstract class is utilized here to apply covariance as it has List[+T] with it where the type parameter T is covariant.
- A trait App is used here to speedily change objects into workable programs.
Zebra Horse Snake Lizard
- Scala Tutorial – Learn Scala with Step By Step Guide
- Scala short <(x: Short): Boolean
- Scala short <(x: Char): Boolean
- Scala Extractors
- Scala | Partially Applied functions
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Keywords
- Scala Int /(x: Int) method with example
- Scala Int /(x: Short) method with example
- Program to print Java Set of characters in Scala
- Scala Map size() method with example
- Scala SortedMap addString() method with a start, a separator and an end with example
- Scala Iterator addString() method with example
- Scala String substring(int beginIndex, int endIndex) method with example
- Scala | Functions Call-by-Name
- Program to convert Java list to an iterator in Scala
- Scala Set &() method with example
- Scala | Type Inference
- Program to convert Java set of Shorts to an Indexed Sequence in Scala
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.