This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Code call (Inheritance) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Code call (Inheritance)" Watch "Code call (Inheritance)" New topic
Author

Code call (Inheritance)

Wu Wen
Greenhorn

Joined: Oct 14, 2011
Posts: 27

The question is How can you let perform_work() method of A to be called from an instance method in C?
And the right answer is There is no way to go more than one level up for methods.

I thought I could use ( (A) this ).perform_work( ) in C to call A's perform_work(), but I got StackOverflowError.
Can anyone explains me why it is not working?

Thanks
Tomasz Sochanski
Ranch Hand

Joined: Jan 13, 2009
Posts: 47

Wu Wen wrote:
I thought I could use ( (A) this ).perform_work( ) in C to call A's perform_work(), but I got StackOverflowError.
Can anyone explains me why it is not working?


Wu - please provide your code here, there are no exceptions runnig ie. this code:

Wu Wen
Greenhorn

Joined: Oct 14, 2011
Posts: 27
Here it is
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4242
    
    7

You can't do what you're trying to do. You can cast to a superclass, but polymorphism means that you'll still be calling the version of perform_work() in the subclass. Which means that in your code the method is calling itself, which leads to a stack overflow.

The only way to call the superclass method is via super.perform_work(), but that only calls the version in the immediate superclass.
Wu Wen
Greenhorn

Joined: Oct 14, 2011
Posts: 27
Matthew Brown wrote:You can't do what you're trying to do. You can cast to a superclass, but polymorphism means that you'll still be calling the version of perform_work() in the subclass. Which means that in your code the method is calling itself, which leads to a stack overflow.

The only way to call the superclass method is via super.perform_work(), but that only calls the version in the immediate superclass.


I get it. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code call (Inheritance)
 
Similar Threads
Mock question
how to using the methods of superclass?
How to call "super.super.methodName()"
Accessing the base class methods in child class.
call base class method