This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes reverse Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reverse Inheritance" Watch "reverse Inheritance" New topic
Author

reverse Inheritance

Rubbal Bhusri
Ranch Hand

Joined: Dec 30, 2010
Posts: 71
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

Joined: Apr 06, 2010
Posts: 4370
    
    8

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

Joined: Oct 02, 2003
Posts: 11255
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
And what do you think reverse inheritance means? I have never heard of it.
Rubbal Bhusri
Ranch Hand

Joined: Dec 30, 2010
Posts: 71
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

Joined: Oct 13, 2005
Posts: 38467
    
  23
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

Joined: Dec 30, 2010
Posts: 71
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

Joined: Oct 13, 2005
Posts: 38467
    
  23
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”
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reverse Inheritance