• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reverse Inheritance

 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mam Kathy Sierra says that reverse Inheritance doesn't exists, at the same time when we do polymorphism , the reverse inheritance comes into scene.
E.G.
class Animal {
// some instance variables
// some methods
}
class Dog extends Animal {
// some instance variables specific to Dog class
void make_noise(){

} //some methods specific to Dog class

}

class Tester {
Animal animal = new Dog();
animal.makenoise();


}
My doubt is that, if reverse inheritance is not possible, then how the above code is valid ???
I shall be very pleased if someone could make me understand this !!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rubbal. Welcome to the Ranch!

What do you mean by reverse inheritance? That code isn't in fact valid, so I'm not quite sure what you're getting at. That wouldn't compile, because makenoise() (and even make_noise()) isn't defined in the Animal class. So it can't be called on an Animal reference.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't valid code (I corrected your makenoise vs make_noise error):


You have an Animal reference. You can only call methods defined in the Animal class (or its parents).

Can you post and example of what you mean that actually compiles and runs?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what do you think reverse inheritance means? I have never heard of it.
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically there's no word called Reverse Inheritance, but In-order to convey that inheritance goes only in one direction i.e. the features can be passed only to children not back from children to parents, Mam Kathy Sierra described in her book Head First Java that Superclass cannot use methods of its Subclasses(which can be seen as reverse inheritance).

But

when the Polymorphism topic came, there she gave an example where we invoked methods of subclass(i.e. class Dog) with the reference variable of Superclass(i.e. class Animal ) as in :

Animal animal = new Dog();
animal.makenoise();

when "animal" is a reference variable of Superclass(i.e. class Animal).

Isn't this contradicts ???
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don’t call her Mam Kathy Sierra.

That is nothing to do with reversing. You declare the reference as an Animal and the instances might be Cat instances or Dog instances. Like here. You can only call methods which exist in the Animal class, however. If Animal hasn’t got chewBone() methods, then you can’t call it. Not even if you have a Dog instance and the Dog class has a chewBone() method.
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i was calling her mam out of respect not in negative sense
I got your point.
But
I really thank you all so much for being so helpful in giving me a good start. You all guys are wonderful.
Again Thank you so much : Campbell Ritchie, Bear Bibeault, fred rosenberger, Matthew Brown.

Hail coderanch.com
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rubbal Bhusri wrote:Actually i was calling her mam out of respect not in negative sense . . . Thank you so much . . .
Explanation accepted (I think I was mistaken about Mam) and “you’re welcome”
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic