Skip to main content

Generics

What are generics?

package com.knowbasics

object AboutGenerics extends App {

class Person {
def apply(): String = "This is apply definition of class"
def gender(): String = ???
}
class Man extends Person
class Woman extends Person

/* COVARIANCE
1. In below People class the A is genric that represent another class type
2. While instantiating it expects a defined class in type
3. It would accept any class that derived from the class A in value part
*/
class AllPeople[+A]
val females: AllPeople[Person] = new AllPeople[Woman]
val males: AllPeople[Person] = new AllPeople[Man]
val allPeople: AllPeople[Person] = new AllPeople[Person]

}