• 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

doubt about question 10 in chapter 2 in K&B

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In question 10 (Page 162 ) why is 'C' not a valid answer. I thought everything in inherited from Object. So why Object won't be a valid return type.
Thanks for any help in advance.
 
Debolina Ray
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
found the answer. Has to be a subtype of the declared return type.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I don't have same book. but I think to return an object you just can return a same type object or which is inheritance of it. not a super class.

class Sp(){}
class Su extends Sp {

sp getSp (){ return new Su();} // This is Legal
// su getSu () { return new Sp(); } //this is illegal.
// su getSu () { return new Object(); } //this is illegal too.
}
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Debolina Ray:


In question 10 (Page 162 ) why is 'C' not a valid answer. I thought everything in inherited from Object. So why Object won't be a valid return type.
Thanks for any help in advance.



Hi Debolina,

The question seems to be asking for legal overloading and overriding. C is not a legal overload because the argument did not change. In addition, it's not a legal override because the return type is not a subclass of the return type of the method being overriden.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Debolina Ray:
In question 10 (Page 162 ) why is 'C' not a valid answer. I thought everything in inherited from Object. So why Object won't be a valid return type.
Thanks for any help in advance.



Hi! I don't understand why C isn't a valid answer. As I understand it, A is a valid answer since SCJP is-a Programmer. But every class is-a Object so why isn't C a correct anser?
The question is question 10 page 162 in K&Bs book SCJP 5 study book. Could someone explain, im sure it isn't very difficult but I don't understand.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mikael don't post on such old threads. If you have a doubt, start a new thread...
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also, please remember not everyone has, or has easy access to, the book. It's legal to post the full question, as long as you cite the source. many folks will look at this and say "well, i'd LIKE to help, but since I have no idea what the questions says, i'll just move along to something else."
 
reply
    Bookmark Topic Watch Topic
  • New Topic