Covariant Return Type in Java
source link: https://www.thejavaprogrammer.com/covariant-return-type-in-java/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Covariant Return Type in Java
In this article, we will learn about covariant return types in Java.
Before Java version 5.0, we could not change the return type of an overridden method but a new method was introduced in JDK 5.0, called covariant where we could change the return type of the overridden method. The return type must be non-primitive. (i.e., it should be a sub-type of the overridden class).
Program:
Output:
Explanation:
In the above program, we created three classes: class ‘Employee’, class ‘Manager’, and class ‘Main’.
In class ‘Employee’, we defined a function, namely ‘work’ and its return type is its class type, i.e., ‘Employee’. In class ‘Manager’, we defined a function whose name is ‘work’ and its return type is its class type ( ‘Manager’ ).
In this case, the Class Manager’s work() method overrides the class Employee’s work() method. And, both methods have a different return type.
‘this’ returns the current object’s class reference. class Employee’s work() method returns ‘Employee’ class type value whereas class Manager’s work() method returns ‘Manager’ class type value. In the third class ‘Main’, we created an object of class ‘Manager’ (‘Manager’ inherits the properties of class ‘Employee’) and call the method using its object. Now, it always prints the class ‘Manager’ function statements instead of class ‘Employee’ because class ‘Manager’ overrides the other class.
How are Covariant return types implemented?
Java doesn’t allow you to change the return type of overridden method but JVM always allows you to change the return type of overridden methods. JVM includes return type in addition to argument types of Employee method for resolution. Classes can have two or more methods, differing only by their return type.
The major advantage of covariant return type is that it reduces the downcasting from the parent class to the child class. It prevents errors at runtime such as ClassCast Exceptions.
So, that’s all about the covariant of return types in Java.
Recommend
-
41
-
35
Today we have another guest post by Alfredo Correa. In his previous article, Functions of variants are covariant , Alfredo described the co...
-
39
Introduction The static special class name in PHP refers to the class a method was actually called on, even if the method is inherited. This is known as “late static binding” (LSB). This RFC...
-
13
A frequent API design problem is the inability to use a more specific return type when overriding a method. A good example of this is your typical Clone method. public abstract Request Clone(); In a sub...
-
4
-
3
I’m sitting here reading through the “What’s New in C#9” blog post, and one of the listed additions to the language is Covariant return types. It...
-
7
Type System What are types: 意义的逻辑含义 一个契约,要求所有成员有共同的特性 所有值、变量都有type 所有对象object都有type 所有class、trait都是type
-
2
【JAVA冷知识】什么是逆变(contravariant)与协变(covariant)?数组支持协变&逆变吗?泛型呢? 生活不能等待别人来安排,要自己去争取和奋斗;而不论其结果是喜是悲,但可以慰藉的是,你总不枉在这世界上活了一场。有了这样的认识,你就会珍重生活,...
-
7
C# - Tips and Tricks 01 - Covariant return types 10 days ago August 27th, 2022 c-sharp ...
-
7
Covariant raises $75M for its AI-powered warehouse robots
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK