Win a copy of Spring Boot in Practice this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

SCJP 5.0 K&B Book,pg 162 Qn.10

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question goes like this
class Programmer {
Programmer debug() { return this; }
}
class SCJP extends Programmer {
// insert code here
}
Which, inserted at line 5 will compile?
A. Programmer debug() { return this;}
B. SCJP debug() { return this;}
C. Object debug() { return this;}
D. int debug() { return 1;}
E. int debug(int x) { return 1;}
F. Object debug(int x) { return this;}

I know A & B are correct. Can somebody explain the other 4 options?
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
E & F are also correct becauase they are overloaded version of the debug() method
 
Kalai Selvi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could somebody please explain about the options C & D?
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Could somebody please explain about the options C & D?



The explanation comes in the book where you get this question -> Covariant Returns.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the readers who don't have the book.

As from Java 1.5. it is allowed to change to return Type of the overriding method when the new type is a subtype of the return type of the overridden method.

In the question Object en int are no subtypes of Programmer. So it's not a legal override in Java 1.5.
reply
    Bookmark Topic Watch Topic
  • New Topic