Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Doubt in K&B SCJP 5: topic Covariant types"  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In Page 169(K&B) problem number 8,solution to the same gives Answer D as one of the solution.I think that is wrong since the question asks "if inserted at line 6 ,will compile".

class Plant
{
String getName(){return "plant";}
Plant getType(){return this;}
}
class Flower extends Plant
{
//line 6}
class Tulip extends Flower{
//line 9
}


The options are:
a)Flower getType(){return this;}
b)String getType(){return this;}
c)Plant getType(){return this;}
d)Tulip getType(){return this;}


If the option D is inserted at line 9 the above code will compile but not line 6.

Is my understanding right?

Thanks in advance
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you misread the question. Option D is "Tulip getType() { return new Tulip(); }", not "Tulip getType() { return this; }". This is perfectly valid. (Note that there's nothing wrong with referring to the Tulip class prior to its actual declaration in the source file.)
 
Meera Nanda
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kelvin!!!
You are right i misread the question!!!
Thank you once again!!
 
I can't take it! You are too smart for me! Here is the tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!