Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling Superclass version of an Overridden Method

 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My method is passed an object whose type is not known until runtime. I know it must be a subclass of a system class e.g. java.awt.Component. I want to execute the standard API version of its method e.g. Component.getLocation() rather than some overridden version implemented by the object itself. Should I do this with reflection API? Are there any other alternatives?
 
Manish Singh
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How will you do it using reflection
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From within the subclass itself, you can do this with super:

But from the "outside", there's no easy way to do that, other than reflection.

But why do you need to do this? It sounds like there might be something wrong with the design or architecture of your application if you feel you need to do this - do you not trust what the getLocation() method of the subclass does, or does the getLocation() method of the subclass do something else than it's supposed to do?
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some legacy code have changed the sematic of Component.getLocation() into relative to the Applet rather than the parent. Now I am writing a system monitoring/debugging tool for my appli. and I want to know the location using the original getLocation() semantic. I don't want to touch any existing code/recompile anything. It would be a big deal I don't want to get involved.

So, this can be done by reflection?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alec Lee wrote:So, this can be done by reflection?

Yes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic