This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
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

A doubt in overriding

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i am studying for SCJP...and this is a question that i saw in SCJP study guide

Given:

1. class Programmer {
2. Programmer debug() { return this; }
3. }
4. class SCJP extends Programmer {
5. // insert code here
6. }

Which, inserted at line 5, will compile? (Choose all that apply.)

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; }


the answer to the question which i made sure after compiling is a,b,e & f
I assume the reason why 'a' is chosen as one of the answers is that Programmer is the superclass of SCJP...in that sense why cant we take c as an answer as Object is also a superclass of SCJP??is it not a suitable overridin function??
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In 1.5 covariant returns allow you to refine the return type of a method by using a subtype of the original return type.

However, you can't move up in the inheritance chain.

If it were allowed, you could have the following.

Class A has a method that returns a FileReader.

Class B extends A and refines the return type of the method to return an Object.

Class C extends B and refines the return type of the method to return a String.
 
vijai kris
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much for clearing my doubt ........got another doubt also while reading now...its about autoboxing & boxing...its said in the book that we cant widen after boxing...therefore we cant convert an int to a Long....but if i box the int to Integer and then widen ...i should be able to convert it to Long right?? ..like i just cant get the difference between widen then box and box then widen....
 
reply
    Bookmark Topic Watch Topic
  • New Topic