Covariant Return Type in Java
Object oriented programming (OOP) has a principle named
substitutability. In this tutorial, let us learn about substitutability
and support for covariant return type in Java. Covariant return type
uses the substitutability principle.
Let T be a super type and S be its subtype (parent and child class). Then, instances (objects) of T can be substituted with instances of S. Parent’s instances can be replaced with the child’s instances without change in behavior of the program.
Let WildAnimal be a supertype and Elephant be a subtype, then an instance obj1 of WildAnimal can be replaced by an insance obj2 of Elephant.
In class Zoo, the method getWildAnimal returns ‘WildAnimal’ which is a super type. AfricaZoo extends Zoo and overrides the method getWildAnimal. While overriding, the return type of this method is changed from WildAnimal to Elephant. This demonstrates covariant type / Liskov substitution principle. We are replacing the supertype’s (WildAnimal) instance with subtype’s (Elephant) instance. This was not possible before JDK 1.5 IndiaZoo is just another example which demonstrates the same covariant type.
Liskov Substitution Principle
Substitutability was introduced by eminent Barbara Liskov and Jeannette Wing. It is also called as Liskov substitution principle.Let T be a super type and S be its subtype (parent and child class). Then, instances (objects) of T can be substituted with instances of S. Parent’s instances can be replaced with the child’s instances without change in behavior of the program.
Let WildAnimal be a supertype and Elephant be a subtype, then an instance obj1 of WildAnimal can be replaced by an insance obj2 of Elephant.
Covariant, Contravariant and Invariant
The subtyping principle which we discussed above as Liskov principle is called covariant. The reverse of it (instead of child replacing the parent, the reverse of it as parent replacing the child) is called contravariant. If no subtyping is allowed then, it is called invariant.Covariant Type in Java
From the release of JDK 1.5, covariant types were introduced in Java. Following example source code illustrates the covariant types in java. In the below example, method overriding is used to demonstrate the covariant type.In class Zoo, the method getWildAnimal returns ‘WildAnimal’ which is a super type. AfricaZoo extends Zoo and overrides the method getWildAnimal. While overriding, the return type of this method is changed from WildAnimal to Elephant. This demonstrates covariant type / Liskov substitution principle. We are replacing the supertype’s (WildAnimal) instance with subtype’s (Elephant) instance. This was not possible before JDK 1.5 IndiaZoo is just another example which demonstrates the same covariant type.
class
WildAnimal {
public
String willYouBite(){
return
"Yes"
;
}
}
class
Elephant
extends
WildAnimal {
public
String whoAreYou() {
return
"Elephant"
;
}
}
class
BengalTiger
extends
WildAnimal {
public
String whoAreYou() {
return
"Bengal Tiger"
;
}
}
class
Zoo {
WildAnimal getWildAnimal() {
return
new
WildAnimal();
}
}
class
AfricaZoo
extends
Zoo {
@Override
Elephant getWildAnimal() {
return
new
Elephant();
}
}
class
IndiaZoo
extends
Zoo {
@Override
BengalTiger getWildAnimal() {
return
new
BengalTiger();
}
}
public
class
Covariant {
public
static
void
main(String args[]){
AfricaZoo afZoo =
new
AfricaZoo();
System.out.println(afZoo.getWildAnimal().whoAreYou());
IndiaZoo inZoo =
new
IndiaZoo();
System.out.println(inZoo.getWildAnimal().whoAreYou());
}
}
0 comments:
Post a Comment