Covariant Method Overriding Of Coffee Five - Coding Best Practices

Sometime cognition of a specific Java characteristic tin flaming amend code quality, Covariant method overriding is i of such feature. Covariant method overriding was introduced inward Java 5, but it seems it lost betwixt other to a greater extent than powerful features of that release. Surprisingly non many Java programmer knows almost Covariant overriding, including myself, until I read, i of the best Java majority on Collection framework, Java Generics in addition to Collection. Covariant method overriding helps to take away type casting on customer side, past times allowing you lot to provide subtype of genuinely provide type of overridden method.

Covariant method overriding tin flaming hold upwards genuinely useful, spell overriding methods which returns object e.g. clone() method. Since clone() provide object every customer needs to shape on to appropriate subclass, non whatsoever more.

By using Java v covariant overriding, nosotros tin flaming straight provide subtype instead of object, equally nosotros volition seen inward examples of this article. This characteristic is non a star characteristic similar Generics or Enum, but it's definitely something worth knowing, given overriding methods are integral business office of Java programming.

I receive got discussed a fleck almost this characteristic before inward difference betwixt overriding in addition to overloading article, in addition to hither I volition exhibit brace of to a greater extent than examples to justify it's usage.





Covariant Method Overriding Example

As I said, i of the best instance of this is overriding clone() method, which is declared inward java.lang.Object flat in addition to has a provide type of Object. If you lot receive got used java.util.Date inward your projection in addition to has called it's clone method to brand a copy, you lot mightiness know that you lot demand to shape it dorsum to Date as shown below :

Date d = new Date(); Date clone = (Date) d.clone();

wouldn't it hold upwards peachy if clone() method tin flaming provide Date instead of Object? Well it tin flaming from Java v onwards, but unfortunately code inward java.util.Date is non notwithstanding updated to receive got wages of this, at to the lowest degree till Java 6. Now let's compass our ain flat in addition to override clone() method to demonstrate role of covariant method overriding inward Java.

import java.util.Date;  /**    * Java plan to demonstrate how to role covariant method overriding to avoid   * casting at customer side.     * @author http://javarevisited.blogspot.com   */ public class CovariantMethodOverridingTest {      public static void main(String args[]) {                //without covariant overriding, shape at customer side needed         Date d = new Date();         Date clone = (Date) d.clone();  //casting required                 //with covariant method overriding, no customer side cast         Duck duck = new Duck(0xFFFFFF);         Duck re-create = duck.clone(); //no casting     }     }  class Duck implements Cloneable{     private int color = 0xFFFFFF;         public Duck(int color){         this.color = color;     }         public int getColor(){         return color;     }      @Override     protected Duck clone() {         Duck clone = null;         try{             clone = (Duck) super.clone();                     }catch(CloneNotSupportedException ex){             throw new RuntimeException(ex);         }                 return clone;     }       }

You tin flaming run across inward our essay program, nosotros don't demand to type shape object returned from clone() method to Duck, because instead of java.lang.Object, its returning java.util.Date. This is too obvious from looking at overriding clone() method inward Duck class, you lot tin flaming run across its provide type is Duck, in addition to non Object. In fact, if you lot receive got implemented your ain Comparator or Comparable inward Java, you lot tin flaming too run across that their compare() in addition to compareTo() straight provide the type specified inward Type parameter instead of object. Though this looks a real pocket-size in addition to subtle change, it's touching is huge. I am certain they volition implement clone() method of java.util.Date sooner or afterward to receive got wages of this feature, unless they compass upwards one's heed to deprecate it inward favour of novel Date in addition to Time API coming inward Java 8.


That's all on covariant method overriding of Java 5 guys. Always receive got wages of this feature, spell overriding method inward Java 5, half dozen ,7 in addition to real shortly on Java 8. You tin flaming provide whatsoever subtype, based upon your need. All other rules of method overriding remains same. I know Java is vast, in addition to at that topographic point are many to a greater extent than features similar this, which tin flaming hold upwards genuinely handy on 24-hour interval to 24-hour interval chore but many times remains hidden from large seat out of Java developers. I too propose reading Java Generics in addition to Collection, a must read majority for whatsoever senior Java developer. I ever rest inward search for hidden gems of Java programming language, API or third-party library which tin flaming brand your task easy, tin flaming amend code lineament in addition to add together value past times whatsoever means. If you lot come upwards across whatsoever such feature, which you lot similar to portion amongst us, become ahead in addition to comment almost it.

Further Learning
Complete Java Masterclass
Difference betwixt static in addition to dynamic binding inward Java
Method Overloading best practices inward Java
Difference betwixt bounded in addition to unbounded wildcards inward Generics


Sumber https://javarevisited.blogspot.com/

0 Response to "Covariant Method Overriding Of Coffee Five - Coding Best Practices"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel