This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

covariant return - easy example .

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This code will not give any compilation error in 1.5 . Right ?
Because Second is a subclass of First . And in 1.5 you can override method just by change the return type ( ofcourse it should be compatable with original ) .

Is everything is right ?
Is this is sufficient for covariant return in beta exam ?

thanks a lot .
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the code above is legal in 1.5 as long as the return type of an overriding method is a subclass of the return type of an overridden method.


So what is the output for the above code?

There is an article on covariant parameter types.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the output will be :
Second: my()

Logic is same as other overridding methods ...
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Joyce ...
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the output will be :
Second: my()

Logic is same as other overridding methods ...

Correct.
 
Christian Joseph
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yap second.my coz its overriding..

thanks for this post, i understand it now after a 15 minutes of searching the net!
 
Bryan Cut
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for answering this. I am prepping for the SCJP6 and this was touched on in the text
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic