• Post Reply Bookmark Topic Watch Topic
  • New Topic

question about inheritance  RSS feed

 
Samuel Weston
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if class Food has the method called 'eat'
and class Potpie inherits from class Food
class Potpie will now have the method eat

now if Potpie has its own method called 'chew'
Potpie will now have 2 methods. eat and chew

now there's a third class called Tuna
if Tuna inherits from Potpie, then
Tuna inherits both the method eat and the method chew.

here's my question:
is class Tuna inheriting method eat from class Food
and method chew from class Potpie
or is class Tuna inheriting both method eat and method chew
from class Potpie alone?

2nd:
how will class Tuna be affected by overide-ation in class Potpie?
will it inherit the original method as it is in class Food
or will it inherit the method as it was changed in class Potpie?

 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Tuna extends Potpie, Tuna will simply inherit the methods from Potpie. This means that if Potpie has overridden the implementation of the eat method from Food,
then Tuna will have that same overridden implementation, unless Tuna overrides the method with its own implementation.

Of course, this rises the question about why a Tuna would be a subclass of Potpie. After all a Tuna is NOT a Potpie.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should a Food class have an eat method? Surely it would be an Animal class which has eat.

That question looks like a don't sweat it question. You would have to go through the Java Language Specification to find out what the official answer is.

As for overridden methods
 
Samuel Weston
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for the replies, you've answered my question
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

And was the eat method inherited from Food or Pie?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!