• 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

reverse Inheritance

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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”
 
reply
    Bookmark Topic Watch Topic
  • New Topic