• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt about question 10 in chapter 2 in K&B

 
Debolina Ray
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
found the answer. Has to be a subtype of the declared return type.
 
Arjun Cheng
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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.
}
 
Jart Bo
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • 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.
 
Mikael T Andersson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mikael don't post on such old threads. If you have a doubt, start a new thread...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic