• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method Inheritance  RSS feed

 
Jim Swainston
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a class named CharStack and a class named PrintableCharStack (working through the scjp book ;) ). PrintableCharStack extends CharStack so inherits all of its public and protected members. However, I'm having trouble getting a client class to compile because of the way I'm instantiating a PrintableCharStack object.



I'm getting an error that says the method setStack() is undefined for the type CharStack. I thought the object cs2 would be treated as a PrintableCharStack object as this is the type after the new operator? I know it will work if I just change the type declaration before cs2 to PrintableCharStack but I thought it was better practice to use more generic types in declarations?

Many thanks for your help in advance

Thanks

Jim
 
Pawan Kr Gupta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right that it is better practice to use more generic types in declarations.but it is used when you are using the concept of polymorphism in your code like overriding.In the case of method overriding method present in base class and as well as all child classes and at runtime it is decided that which method has to be called.

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Your variable cs2 holds a reference of type CharStack. That means you can only use cs2 to call methods accessible in CharStack. In other words, even though cs2 references an object that is actually a PrintableCharStack (containing the method, setStack), the type of cs2 is CharStack, which does not have such a method.

One option might be to declare the setStack() method as abstract in CharStack, and defer implementation to subclasses like PrintableCharStack. Then because of polymorphism, when you call the method using a CharStack reference, the implementation in PrintableCharStack will be executed.
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is setStack() also present in- CharStack?

With a reference of type SuperClass- only those methods can be invoked which are present in the SuperClass. Now if you inherit this SuperClass and override these methods in the SubClass, at runtime the implementation in the SubClass is invoked provided the SubClass has overridden those methods. If the SubClass adds a new method not present in the SuperClass then using the reference of type SuperClass you cannot invoke the method define in SubClass only. This is because the method bindings happen at the Compile time which uses the reference type and the selection of the method to be invoked happen at the Runtime which also looks at the type of object alloted to the reference.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Jim.
What is the access modifier for CharStack#setStack()?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Swainston wrote:......

Note: I'm taking the above comment to mean that setStack is not in the parent class, CharStack -- but is instead declared in the subclass, PrintableCharStack. If that's the case, then there is no inheritance. That's the assumption I based my above response on.
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc weber wrote:
Jim Swainston wrote:......

Note: I'm taking the above comment to mean that setStack is not in the parent class, CharStack -- but is instead declared in the subclass, PrintableCharStack. If that's the case, then there is no inheritance. That's the assumption I based my above response on.


Same assumption here!
 
Jim Swainston
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey All, thanks for the help!

You've assumed right, setStack is only declared in the subclass PrintableCharStack. I think I'm getting closer to understanding what's happening.

If I had also declared the setStack method in the parent class CharStack that would make the method overridden. In which case dynamic binding would be applied and the implementation in PrintableCharStack would be called at runtime, being an example of polymorphism.

However, I only declared the setStack method in the subclass. I thought that instantiating the cs2 object in the way I did would make it be both a CharStack and PrintableCharStack object (implicit casting) meaning I could call methods on it that are only declared in the subclass. What have I Misunderstood?

Thanks again.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't understand this:

marc weber wrote:Your variable cs2 holds a reference of type CharStack. That means you can only use cs2 to call methods accessible in CharStack. In other words, even though cs2 references an object that is actually a PrintableCharStack (containing the method, setStack), the type of cs2 is CharStack, which does not have such a method.
 
Jim Swainston
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that at compile time variable cs2 holds a reference of type CharStack. However, I thought that at runtime an object of type PrintableCharStack would be created and late method binding would apply so that the method defined in PrintableCharStack would run.

I think I understand now that if you a declare a reference type you can only use the methods and variables that are declared in that class. If I had also defined setStack in the CharStack class then my client class would have compiled and the implementation of setStack defined in PrintableCharStack would have been executed at runtime due to late method binding as setStack would be an overridden method.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!