• 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

simple inheritance question (with code)

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The questions are in the comments in the following program:



Thank you!
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why is this calling B.doStuff() and not A.doStuff() ..?


Because of polymorphism.

Can I call A.doStuff() from C?


Yes, you can. new A().doStuff() will do the trick.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello George.
You need to check up on inheritance, polymorphism and instance methods. In brief, although you are invoking the doStuff() method on an object reference of type A, at runtime the JVM knows that the actual type of the instantiated object is B, and it invokes B's version of the method.
This feature applies to instance methods which can be overridden (as here) and not to static methods or variables (which is why your code prints the value of A's x variable).
I'm sure others can provide fuller explanations...
 
George Panadis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the responses.

And let me rephrase my question,

I am basically asking if I can access the super class' overridden method by using a subclass object.

So in the link provided above, the Dog class has its own play() method that overrides the Animal's play() method (Dog extends Animal) What if i wanted a Dog to "play" like an Animal instead? Is that possible?
 
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
George,

I am basically asking if I can access the super class' overridden method by using a subclass object.



You can do that by calling doStuff() defined in class A, from doStuff() defined in class B:



Please excuse me if I got the wrong end of the stick here. Is this what you have been trying to do?

With respect,
Mala
 
George Panadis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mala,
not exactly.
By using super, A.doStuff() will always be called when B.doStuff() is called.
I am trying to call A.doStuff() instead of B.doStuff() by using a B object in another class (such as C).

This might not make sense because it is illegal? Im not sure!

So, as I am "overriding" the integer variable x by casting an A object :



I wanna know if I can access overridden methods of the super class in a similar fashion.
I was expecting the following to accomplish this but it doesnt:

 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a huge difference between variables and methods: methods can be overridden, variables can't (although it looks like it, but a variable in a subclass is just shadowing a variable with the exact same name in the parent class.

That's why in your example ((A)b).x will print 15 (no overriding, A and B just have a variable with the same name), but with method doStuff you have overriding and thus polymorphism. If you have an instance of type B, it will always run the doStuff-method of class B. Casting to A won't help, because which (overridden) method to execute is decided at runtime and at runtime your instance is still from class B (so its method is called).

You can however (like illustrated by Mala) call the method of the parent class by calling super from the method from the child class. Or you can create an instance of class A and invoke its doStuff-method. But creating an

George Panadis wrote:I am trying to call A.doStuff() instead of B.doStuff() by using a B object in another class (such as C).


That's simply impossible!

 
George Panadis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alrighty then.
Thank you much!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic