Covariant Method Overriding Of Coffee V - Coding Best Practices
Sometime cognition of a specific Java characteristic tin give the sack ameliorate code quality, Covariant method overriding is i of such feature. Covariant method overriding was introduced inwards 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 mass on Collection framework, Java Generics together with Collection. Covariant method overriding helps to take away type casting on customer side, past times allowing you lot to furnish subtype of genuinely furnish type of overridden method.
Covariant method overriding tin give the sack hold out genuinely useful, spell overriding methods which returns object e.g. clone() method. Since clone() furnish object every customer needs to shape on to appropriate subclass, non whatever more.
By using Java v covariant overriding, nosotros tin give the sack direct furnish subtype instead of object, equally nosotros volition seen inwards 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 purpose of Java programming.
I conduct maintain discussed a fleck almost this characteristic before inwards difference betwixt overriding together with overloading article, together with hither I volition demo twosome of to a greater extent than examples to justify it's usage.
wouldn't it hold out bully if clone() method tin give the sack furnish Date instead of Object? Well it tin give the sack from Java v onwards, but unfortunately code inwards java.util.Date is non soundless updated to accept wages of this, at to the lowest degree till Java 6. Now let's do our ain flat together with override clone() method to demonstrate usage of covariant method overriding inwards Java.
You tin give the sack encounter inwards our attempt 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 likewise obvious from looking at overriding clone() method inwards Duck class, you lot tin give the sack encounter its furnish type is Duck, together with non Object. In fact, if you lot conduct maintain implemented your ain Comparator or Comparable inwards Java, you lot tin give the sack likewise encounter that their compare() together with compareTo() direct furnish the type specified inwards Type parameter instead of object. Though this looks a rattling pocket-sized together with subtle change, it's affect is huge. I am certain they volition implement clone() method of java.util.Date sooner or afterward to accept wages of this feature, unless they determine to deprecate it inwards favour of novel Date together with Time API coming inwards Java 8.
That's all on covariant method overriding of Java 5 guys. Always accept wages of this feature, spell overriding method inwards Java 5, six ,7 together with rattling shortly on Java 8. You tin give the sack furnish whatever subtype, based upon your need. All other rules of method overriding remains same. I know Java is vast, together with in that place are many to a greater extent than features similar this, which tin give the sack hold out genuinely handy on twenty-four lx minutes menstruum to twenty-four lx minutes menstruum chore but many times remains hidden from large release of Java developers. I likewise propose reading Java Generics together with Collection, a must read mass for whatever senior Java developer. I ever rest inwards search for hidden gems of Java programming language, API or third-party library which tin give the sack brand your task easy, tin give the sack ameliorate code character together with add together value past times whatever means. If you lot come upwardly across whatever such feature, which you lot similar to part amongst us, become ahead together with comment almost it.
Further Learning
Complete Java Masterclass
Difference betwixt static together with dynamic binding inwards Java
Method Overloading best practices inwards Java
Difference betwixt bounded together with unbounded wildcards inwards Generics
Covariant method overriding tin give the sack hold out genuinely useful, spell overriding methods which returns object e.g. clone() method. Since clone() furnish object every customer needs to shape on to appropriate subclass, non whatever more.
By using Java v covariant overriding, nosotros tin give the sack direct furnish subtype instead of object, equally nosotros volition seen inwards 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 purpose of Java programming.
I conduct maintain discussed a fleck almost this characteristic before inwards difference betwixt overriding together with overloading article, together with hither I volition demo twosome 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 inwards java.lang.Object flat together with has a furnish type of Object. If you lot conduct maintain used java.util.Date inwards your projection together with has called it's clone method to brand a copy, you lot powerfulness 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 out bully if clone() method tin give the sack furnish Date instead of Object? Well it tin give the sack from Java v onwards, but unfortunately code inwards java.util.Date is non soundless updated to accept wages of this, at to the lowest degree till Java 6. Now let's do our ain flat together with override clone() method to demonstrate usage of covariant method overriding inwards Java.
import java.util.Date; /** * Java plan to demonstrate how to usage 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 give the sack encounter inwards our attempt 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 likewise obvious from looking at overriding clone() method inwards Duck class, you lot tin give the sack encounter its furnish type is Duck, together with non Object. In fact, if you lot conduct maintain implemented your ain Comparator or Comparable inwards Java, you lot tin give the sack likewise encounter that their compare() together with compareTo() direct furnish the type specified inwards Type parameter instead of object. Though this looks a rattling pocket-sized together with subtle change, it's affect is huge. I am certain they volition implement clone() method of java.util.Date sooner or afterward to accept wages of this feature, unless they determine to deprecate it inwards favour of novel Date together with Time API coming inwards Java 8.
That's all on covariant method overriding of Java 5 guys. Always accept wages of this feature, spell overriding method inwards Java 5, six ,7 together with rattling shortly on Java 8. You tin give the sack furnish whatever subtype, based upon your need. All other rules of method overriding remains same. I know Java is vast, together with in that place are many to a greater extent than features similar this, which tin give the sack hold out genuinely handy on twenty-four lx minutes menstruum to twenty-four lx minutes menstruum chore but many times remains hidden from large release of Java developers. I likewise propose reading Java Generics together with Collection, a must read mass for whatever senior Java developer. I ever rest inwards search for hidden gems of Java programming language, API or third-party library which tin give the sack brand your task easy, tin give the sack ameliorate code character together with add together value past times whatever means. If you lot come upwardly across whatever such feature, which you lot similar to part amongst us, become ahead together with comment almost it.
Further Learning
Complete Java Masterclass
Difference betwixt static together with dynamic binding inwards Java
Method Overloading best practices inwards Java
Difference betwixt bounded together with unbounded wildcards inwards Generics
0 Response to "Covariant Method Overriding Of Coffee V - Coding Best Practices"
Post a Comment