Java Covariant Return Type = Method Overriding

What Is Java Covariant Type?

Howdy Mowdy People? Have you ever override a method by changing its return type? Well for y’all that is already a pro in Coding may know this type of overriding, but for those who is still  a green horn in coding, just like me :’D maybe. This kind of method overriding is called Java Covariant Return Type, which is not available until Java5 came out.

Here ok, let me just show you a simple presentation of this Covariant Type :


public class A{  
A get(){return this;}  
}  
  
class B extends A{  
B get(){return this;}  
void message(){System.out.println(“Craftingez6 covariant return type“);}  
  
public static void main(String args[]){  
new B().get().message();  
}  
}  

This will output :

Craftingez6 covariants return type

Well as you can see, the return type of get() method A class is A, but then… the return type of B class is.. B. Both of this class have a different return type but both of them is tied in a method overriding, B overriding A.This thing/process/phenomenon is called Covariant return type.

There we go I’ve explaing it. See you!

Conclusions

  • Java Covariant Return Type is Method overriding 
  • Both of this class have a different return type but both of them is tied in a method overriding, B overriding A

Published by

Rafif Santika

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.