• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Code call (Inheritance)

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 47
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here it is
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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'm gonna teach you a lesson! Start by looking at this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic