aspose file tools*
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
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: 4392
    
    8

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.
 
jQuery in Action, 2nd edition
 
subject: Code call (Inheritance)