• 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

Help??? Is this a case of overriding?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just came across this question in my preparation for SCJP2.
Here is the code:
abstract class Mammal{
public Mammal(){}
abstract Mammal giveBirth(); }
class Dog extends Mammal{
public Dog (){}
//here is the overriding version of giveBirth()
Dog giveBirth(){}
}//class Dog ends.
I believe the above subclass version of giveBirth() is a legal case of overriding although the return type is subtype of the one declared in parent.
Any idea or comments?
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jason:
I just came across this question in my preparation for SCJP2.
Here is the code:
abstract class Mammal{
public Mammal(){}
abstract Mammal giveBirth(); }
class Dog extends Mammal{
public Dog (){}
//here is the overriding version of giveBirth()
Dog giveBirth(){}
}//class Dog ends.
I believe the above subclass version of giveBirth() is a legal case of overriding although the return type is subtype of the one declared in parent.
Any idea or comments?


IMHO this would be an example of allowed overridding, A mammal reference can be used to refer to a Dog object so...
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a very interesting question! Could you please try compiling the code (perhaps making Mammel non-abstract) and see what happens when you make polymorphic calls??
I've only ever read that the return type has to "match" between the super and sub versions, but, intuitively, it seems that a subclassed return type should be fine. This would be preferred over having to cast the returned Mammal to a Dog or such.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say you are not overriding anything you are implementing abstract method, although the compiler will complain that it is not valid overriding since the return type has to be identical.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm, I think that when you implement an abstract method, you're are overriding. I associate overriding more with dynmaic binding than with method implementing. That's just my take on it.
Man, it's a let down (but certainly not unexpected) that the compiler complains about the unmatched--yet related--return types. Perhaps, more thought into the matter will prove that to be the better approach, though.
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly, since mammal is abstract, this would not be an override, rather an implementation.But this is trivial to the question, I guess.
In case of overriding then IMHO, I dont suppose this is legal. Although Dog Extends mammal,I dont suppose you can override Mammal giveBirth() with Dog giveBirth() in as far as overriding rules are concerned. A better, and legal implementation/override would be
class Dog extends Mammal{
public Mammal giveBirth(){
return new Dog();
}
}
So even if the return type is declared as mammal, we are allowed to return its subclass, inside dog.
Herbert

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My thinking was that just because you're implementing an abstract function, that doesn't mean that you're not overriding.
Yeah, you could return the Mammal, but, like I stated earlier, it'd be cool (and more reflective of the real world) if you didn't have to do that.
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I compiled the code in JasonOverride.java and here is the result:
JasonOverride.java:10: The method Dog giveBirth() declared in class Dog cannot override the method of the same signature declared in class Mammal. They must have the same return type.
Dog giveBirth(){}
^
1 error
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The return type must be exactly same. Here is an interesting point:
Mammal giveBirth(){ return new Dog()};
it compiles perfectly..The point to think is return
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
It compiles as per following:
class Dog extends Mammal{
public Mammal giveBirth(){
return new Dog();
}
}

If we execute this as 'Dog giveBirth()',it clearly complains about overriding rule.
Regarding An abstract method :

--- An Abstract method is an implementation placeholder.
--- They are part of an abstract class.
--- They must be

overridden

by a concrete subclass.
--- Each concrete subclass can implement the method differently.

By the by What is IMHO
Hope this helps.
Any Questions or corrections please
Thanks
Nirmala
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well, I try both with Mammal as an abstract class and not an in both cases,
Dog.java:11: The method Dog giveBirth() declared in class Dog cannot override the method of the same signature declared in class Mammal. They must have the same return type.
So I stand corrected Thanks
IMHO is a chat acronym it means 'In My Humble Opinion' There are a lot of them to find out more RTFM

[This message has been edited by Carl Trusiak (edited July 11, 2000).]
[This message has been edited by Carl Trusiak (edited July 11, 2000).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic