| | |
Covariant return types
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I'm been working through my textbook and just can't seem to understand the section on covariant return types. ie) what are covariant return types, what they are used for, etc. The definition in the book is as follows. Any explanation, link to a simple tutorial or example would be greatly appreciated!
The example from the text. What is the author trying to get across?
•
•
•
•
An overridden method in a derived class can return a type derived from the type returned by the base-class method.
The example from the text. What is the author trying to get across?
Java Syntax (Toggle Plain Text)
//: polymorphism/CovariantReturn.java class Grain { public String toString() { return "Grain"; } } class Wheat extends Grain { public String toString() { return "Wheat"; } } class Mill { Grain process() { return new Grain(); } } class WheatMill extends Mill { Wheat process() { return new Wheat(); } } public class CovariantReturn { public static void main(String[] args) { Mill m = new Mill(); Grain g = m.process(); System.out.println(g); m = new WheatMill(); g = m.process(); System.out.println(g); } } /* Output: Grain Wheat *///:~
Last edited by VinC; Nov 12th, 2006 at 4:22 pm.
![]() |
Similar Threads
- Trouble writing code for arithmetic calculations of complex numbers (C++)
- why return an int from main? (C++)
- Why constructors don't have return types (C++)
Other Threads in the Java Forum
- Previous Thread: 3 to 8 decoder simulation
- Next Thread: Firscup Trouble...
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) calculator chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing threads time tree unlimited webservices windows





