{"id":7869,"date":"2014-06-03T08:41:55","date_gmt":"2014-06-03T08:41:55","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=7869"},"modified":"2021-01-21T10:53:09","modified_gmt":"2021-01-21T10:53:09","slug":"covariant-return-types-in-java","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/covariant-return-types-in-java\/","title":{"rendered":"Covariant Return Types In Java"},"content":{"rendered":"<p>As we already know what method overriding is, the subclass providing new implementation of the super class method provided that the method signature in subclass should exactly match with method signature of super class including the return types.<\/p>\n<p>The covariant return types are newly introduced since Java 5.0, and used during method overriding. Covariant return type allows us to change the return type of the overriding method in the subclass; however this return type in subclass method must be a <i>subtype<\/i> of super class method return type.<\/p>\n<p><strong>Also Check:<\/strong> <a href=\"https:\/\/www.whizlabs.com\/blog\/java-interview-questions-for-freshers\/\" target=\"_blank\" rel=\"noopener\">Java Interview Questions and Answer<\/a><\/p>\n<p>Simply put, overriding method (in subclass) can have a return type that is subtype of overridden method return type (in superclass). If this concept is confusing, it can be explained best with an example:<\/p>\n<p><code><br \/>\nclass Vehicle {<br \/>\npublic Vehicle move(String direction) {<br \/>\nreturn new Vehicle();<br \/>\n}<br \/>\n}<br \/>\nclass Van extends Vehicle {<br \/>\npublic Vehicle move(String direction) {<br \/>\nreturn new Vehicle();<br \/>\n}<br \/>\n}<br \/>\n<\/code><\/p>\n<p>In the above code example, we have two classes Vehicle and Van. The Van class is a subclass of Vehicle by inheritance. Also, we are overriding the method move() in the subclass with exactly same parameters and return type. The Van class is subclass of Vehicle. In other words, Van is a subtype of Vehicle (or Van is a Vehicle). Now, in order to make use of covariant return types, let us change the subclass\u2019s overriding method return type to Van.<\/p>\n<p><code><br \/>\nclass Van extends Vehicle {<br \/>\n\/\/ Notice the covariant return type Van here, which is subtype of Vehicle<br \/>\npublic Van move(String direction) {<br \/>\nreturn new Van();<br \/>\n}<br \/>\n}<br \/>\n<\/code><br \/>\nThe above code compiles with Java 5.0 because covariant return types are only introduced from Java 5.0. If we try to compile above code with Java 1.4 compiler, we get compiler error.<br \/>\n<code><br \/>\njavac -source 1.4 Van.java<br \/>\nattempting to use incompatible return type<br \/>\n<\/code><br \/>\nSince we understood the concept of covariant return types, the following is a full example which demonstrates covariant return type.<br \/>\n<code><br \/>\nclass Vehicle {<br \/>\npublic Vehicle getInstance() {<br \/>\nreturn this;<br \/>\n}<br \/>\n}<br \/>\nclass Van extends Vehicle {<br \/>\n\/\/ Covariant return type in action!!!<br \/>\npublic Van getInstance() {<br \/>\nreturn this;<br \/>\n}<br \/>\npublic void move() {<br \/>\nSystem.out.println(\"Van is moving ...\");<br \/>\n}<br \/>\n}<br \/>\npublic class CovariantTest {<br \/>\npublic static void main(String... args) {<br \/>\nnew Van().getInstance().move();;<br \/>\n}<br \/>\n}<br \/>\n<\/code><\/p>\n<p><strong>Output<\/strong><br \/>\nVan is moving.<\/p>\n<p>The previous code example is improved by adding getInstance() method to Vehicle class and overriding this method in subclass Van. The implementation of this getInstance() method returns &#8216;this&#8217;, which is returns Vehicle instance in the Vehicle class method and Van instance in the overridden Van class method respectively. Notice how the getInstance() method is overridden with covariant return type (Van in this case).<\/p>\n<p>So, the advantage of covariant return types is that, we can have more specific return types when overriding methods. This avoids unnecessary confusing type casts in the class hierarchy thus making the code more readable and maintainable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As we already know what method overriding is, the subclass providing new implementation of the super class method provided that the method signature in subclass should exactly match with method signature of super class including the return types. The covariant return types are newly introduced since Java 5.0, and used during method overriding. Covariant return type allows us to change the return type of the overriding method in the subclass; however this return type in subclass method must be a subtype of super class method return type. Also Check: Java Interview Questions and Answer Simply put, overriding method (in subclass) [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[674],"class_list":["post-7869","post","type-post","status-publish","format-standard","hentry","category-java","tag-covariant-return-types-in-java"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":0,"uagb_excerpt":"As we already know what method overriding is, the subclass providing new implementation of the super class method provided that the method signature in subclass should exactly match with method signature of super class including the return types. The covariant return types are newly introduced since Java 5.0, and used during method overriding. Covariant return&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/7869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=7869"}],"version-history":[{"count":3,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/7869\/revisions"}],"predecessor-version":[{"id":77134,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/7869\/revisions\/77134"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=7869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=7869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=7869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}