Covariant return types in Java Example
Its a very trending interview question now so thought of sharing some of my inputs
on it.
So,before going deep into it.i would like to answer this question very generic ways like
before java JDK verion 5.0.It was not possible to override a method by changing the return type.
When we override a parent class method, the name, argument types and return type of the overriding method in child class has to be exactly same as that of parent class method. Overriding method was said to be invariant with respect to return type.Now if you have notice
two keyword such as Covariant and Invariant So what are those two keywords?.
Covariant return types
Java JDK version 5.0 onwards it is possible to have different return type for a overriding method in child class, but child’s return type should be sub-type of parent’s return type. Overriding method becomes variant with respect to return type.
So all this concept is based on one PRINCIPLE which is Liskov substitution principle.
Now what is this PRINCIPLE?
Let me give breife of this principle.
Liskov substitution principle:
Substitutability is a principle in object-oriented programming stating that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e. an object of type T may be substituted with any object of a subtype S) without altering any of the desirable properties of T (correctness, task performed, etc.).
More formally, the Liskov substitution principle (LSP) is a particular definition of a subtyping relation, called (strong) behavioral subtyping, that was initially introduced by Barbara Liskov in a 1987 conference keynote address titled Data abstraction and hierarchy. It is a semantic rather than merely syntactic relation because it intends to guarantee semantic interoperability of types in a hierarchy, object types in particular.
Subtype Requirement: Let {\displaystyle \phi (x)} \phi (x) be a property provable about objects {\displaystyle x} x of type T. Then {\displaystyle \phi (y)} {\displaystyle \phi (y)} should be true for objects {\displaystyle y} y of type S where S is a subtype of T.
In Layman term,i would rather say this.have a look.
Liskov Substitution Principle,As per the LSP, functions that use references to base classes must be able to use objects of the derived class without knowing it. In simple words, derived classes must be substitutable for the base class. To illustrate the LSP, let’s take an example of rectangles and squares. One tends to establish the ISA relationship, thus, you can say that a square a rectangle. However, there arises a problem (hence, a violation of the LSP).Lets not
go in fuirther details of it.Just keep the above points in your mind for Covariant return types in Java.
Let me provide a simple Code for better understanding:
class A {}
class B extends A {}
class Base
{
A fun()
{
System.out.println("Base fun()");
return new A();
}
}
class Derived extends Base
{
B fun()
{
System.out.println("Derived fun()");
return new B();
}
}
public class Main
{
public static void main(String args[])
{
Base base = new Base();
base.fun();
Derived derived = new Derived();
derived.fun();
}
}
Output:
Base fun()
Derived fun()
Note : If we swap return types of Base and Derived, then above program would not work.
Advantages:
1)It helps to avoid confusing type casts present in the class hierarchy and thus making the code readable, usable and maintainable.
2)We get a liberty to have more specific return types when overriding methods.
3)Help in preventing run-time ClassCastExceptions on returns.
Its a very trending interview question now so thought of sharing some of my inputs
on it.
So,before going deep into it.i would like to answer this question very generic ways like
before java JDK verion 5.0.It was not possible to override a method by changing the return type.
When we override a parent class method, the name, argument types and return type of the overriding method in child class has to be exactly same as that of parent class method. Overriding method was said to be invariant with respect to return type.Now if you have notice
two keyword such as Covariant and Invariant So what are those two keywords?.
Covariant return types
Java JDK version 5.0 onwards it is possible to have different return type for a overriding method in child class, but child’s return type should be sub-type of parent’s return type. Overriding method becomes variant with respect to return type.
So all this concept is based on one PRINCIPLE which is Liskov substitution principle.
Now what is this PRINCIPLE?
Let me give breife of this principle.
Liskov substitution principle:
Substitutability is a principle in object-oriented programming stating that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e. an object of type T may be substituted with any object of a subtype S) without altering any of the desirable properties of T (correctness, task performed, etc.).
More formally, the Liskov substitution principle (LSP) is a particular definition of a subtyping relation, called (strong) behavioral subtyping, that was initially introduced by Barbara Liskov in a 1987 conference keynote address titled Data abstraction and hierarchy. It is a semantic rather than merely syntactic relation because it intends to guarantee semantic interoperability of types in a hierarchy, object types in particular.
Subtype Requirement: Let {\displaystyle \phi (x)} \phi (x) be a property provable about objects {\displaystyle x} x of type T. Then {\displaystyle \phi (y)} {\displaystyle \phi (y)} should be true for objects {\displaystyle y} y of type S where S is a subtype of T.
In Layman term,i would rather say this.have a look.
Liskov Substitution Principle,As per the LSP, functions that use references to base classes must be able to use objects of the derived class without knowing it. In simple words, derived classes must be substitutable for the base class. To illustrate the LSP, let’s take an example of rectangles and squares. One tends to establish the ISA relationship, thus, you can say that a square a rectangle. However, there arises a problem (hence, a violation of the LSP).Lets not
go in fuirther details of it.Just keep the above points in your mind for Covariant return types in Java.
Let me provide a simple Code for better understanding:
class A {}
class B extends A {}
class Base
{
A fun()
{
System.out.println("Base fun()");
return new A();
}
}
class Derived extends Base
{
B fun()
{
System.out.println("Derived fun()");
return new B();
}
}
public class Main
{
public static void main(String args[])
{
Base base = new Base();
base.fun();
Derived derived = new Derived();
derived.fun();
}
}
Output:
Base fun()
Derived fun()
Note : If we swap return types of Base and Derived, then above program would not work.
Advantages:
1)It helps to avoid confusing type casts present in the class hierarchy and thus making the code readable, usable and maintainable.
2)We get a liberty to have more specific return types when overriding methods.
3)Help in preventing run-time ClassCastExceptions on returns.
No comments:
Post a Comment