Scala | Variances Last Updated : 10 Apr, 2019 Comments Improve Suggest changes Like Article Like Report Variance is the interconnection of Sub-Typing relationships which are either of complicated types or of their constituent types. Variance explains inheritance correlation of Types that have parameters or arguments within them. These types belongs to the generic classes, which takes a type like a parameter. In the presence of Variance one can create relations between complicated types and in its absence we won't be able to reiterate the abstraction class. The Scala Variances are of three types, which are as follows: Covariant Contravariant Invariant Some important points: In Scala collection's types can be constructed more securely through Variances. Variances can provide us with some extra adjustable advancements. It also helps in the development of authentic applications. Variances can be applied on any Scala Types like List, Sets, etc. Types of Variances Let's discuss each type in detail. Covariant: If a generic class has a type parameter T, then its Covariant notation will be [+T]. Suppose, we have two List types of Scala i.e, S and T. where, S is sub-type of T, then you can say that List[S] is also the sub-type of List[T]. If two types are related like this then they fall under the Covariant type. List[T] can be called as Generic. Syntax: List[+T] Here, T is the type parameter and + is the symbol of Covariance. Example: Scala // Scala program of covariant // type // Creating an abstract class // for Student abstract class Student { def name: String } // Creating a sub-class Girls // of Student case class Girls(name: String) extends Student // Creating a sub-class Boys // of Student case class Boys(name: String) extends Student // Creating an Object Covariance // that inherits main method of // App object Covariance extends App { // Creating a method def Studentnames(students: List[Student]): Unit = { students.foreach { student => // Displays students name println(student.name) } } // Assigning names val boys: List[Boys] = List(Boys("Kanchan"), Boys("Rahul")) val girls: List[Girls] = List(Girls("Nidhi"), Girls("Geeta")) // Accessing list of boys Studentnames(boys) // Accessing list of girls Studentnames(girls) } Output: Kanchan Rahul Nidhi Geeta Here, List of boys and girls both belongs to the List of students as they are its sub-type and so, here names of all the students are displayed when the Super-type Student is called. Note: 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. Contravariant: If a generic class has a type parameter T, then its Contravariant notation will be [-T]. Suppose, we have two List types of Scala i.e, S and T. where, S is sub-type of T, but List[T] is the sub-type of List[S]. If two types are related like this then they fall under the Contravariant type. It is opposite of covariant. Syntax: List[-T] Here, T is the type parameter and - is the symbol of Contravariance. Example: Scala // Scala program of Variance of // Contravariant type // abstract class with a contravariant // type parameter abstract class Show[-T] { // Method for printing // type T def print(value: T): Unit } // A class structure abstract class Vehicle { def name: String } // Creating sub-class of Vehicle case class Car(name: String) extends Vehicle // Creating sub-class of class // Show class VehicleShow extends Show[Vehicle] { def print(vehicle: Vehicle): Unit = // Displays name of the vehicle println("The name of the vehicle is: " + vehicle.name) } // Creating sub-class of class // Show class CarShow extends Show[Car] { def print(car: Car): Unit = // Displays name of the car println("The name of the car is: " + car.name) } // Inheriting main method of // the trait App object Contravariance extends App { // Assigning value to the name val newCar: Car = Car("Scorpio") // Defining a method that // prints the name def printnewCar(show: Show[Car]): Unit = { show.print(newCar) } // Creating objects val showcar: Show[Car] = new CarShow val showvehicle: Show[Vehicle] = new VehicleShow // Accessing name printnewCar(showcar) printnewCar(showvehicle) } Output: The name of the car is: Scorpio The name of the vehicle is: Scorpio It is Contravariant so, we are able to substitute Show[Vehicle] for Show[Car] and that's why both vehicle and car returns the same name. Invariant: In Scala, generic types are by default invariant. Suppose, we have two List types of Scala i.e, S and T. where, S is sub-type of T but List[T] and List[S] are not at all related, then they fall under the invariant type. Syntax: List[T] Here, we don't use any symbol for invariant relationship. Note: The classes like Array[T], ListBuffer[T], ArrayBuffer[T] etc. are mutable so, they have invariant type parameter, if we use invariant type parameters in inheritance relationship or sub-typing then we will get a compilation error. Comment More infoAdvertise with us Next Article Scala | Variances nidhi1352singh Follow Improve Article Tags : Technical Scripter Scala Scala scala-parameterized-type Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We 9 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are 10 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac 13 min read AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of 4 min read Like