• 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

OO Ques

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K&B book ch-2 Q-10
class Programmer{
Programmer debug(){return this;}
}
class Scjp extends Programmer{
Object debug(){return this;} // line 1
}


Why is'nt line 1 a valid option?
why cant we use Object in the return type?
both Scjp and Programmer extend Object
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why cant we use Object in the return type?
both Scjp and Programmer extend Object



True. But given an Object, is it guarranteed to be a Scjp or Programmer instance?


A Scjp instance IS-A Programmer instance right? So, given an Scjp instance, you should be able to use it as a Programmer instance right? Given that the method returns an Object, instead of a Programmer, can you use the Scjp object just like a Programmer object?

Henry
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can use Object as return type.
But your case is an example of method overriding.

One of the rule of overriding is :-
Return type of the overriding method is either same or subclass of the return type of overriden method ( covarient return type).

compilation error is coming because of this rule.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pradeepta,
You are overriding the debug() method, and in the subclass your return type is higher in inheritance tree than the return type of the parent class method (ie. Object > Programmer).

Only Covariant return types work in overriding--which means that the subclass can have a return type that is a subtype of the return type of the parent class's method. But in your case, covariant types rule in overriding is not abided. So you will get a compiler error.
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yess,
Thanks all
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic