• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

co-variant returns

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In K&B for java 1.5 chap-2,Self-test Q8 i have a doubt...

Which statement(s), inserted at line 6,will compile?
1. Flower getType(){return this;}
2. String getType(){return "this";}
3. Plant getType(){return this;}
4. Tulip getType(){return new Tulip();}
the answers in the book says options 1,3 and 4 are correct as 1 and 4 are examples of co-variant returns
but when i tried to compile the program using these options ...it said the return type is incompatible with Plant.getType()....i am totally confused now! Plz Help


[ March 25, 2006: Message edited by: nivi zal ]

[ March 25, 2006: Message edited by: nivi zal ]
[ March 25, 2006: Message edited by: nivi zal ]
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nivi,

I had no problem compiling the program using options 1, 3, 4.

Run "javac -version" to see if you're using the correct JDK version. Mine is 1.5.0_03.

Joyce
[ March 25, 2006: Message edited by: Joyce Lee ]
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Plant{
String getName(){ return "plant";}
Plant getType(){return this;}
}
class Flower extends Plant{
Flower getType(){return this;}
//Plant getType(){return this;}
//Tulip getType(){return new Tulip();}
}
class Tulip extends Flower{
public static void main(String...args){
Tulip myTulip = new Tulip();
myTulip.getType();
}
}
there is no problem with this it compile...
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi nivi,

Covariant returns are valid only as of Java 5. For certain you're using a previous Java version.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well even if some one is using java 5 as a compiler he can check wheter the code can work for java 1.4 , using the " javac -source 1.4 sourceFile.java " option , so people using 5 can check wheter the code would work for 1.4 or not
 
nivi zal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the help!
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic