File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Calling Superclass version of an Overridden Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling Superclass version of an Overridden Method" Watch "Calling Superclass version of an Overridden Method" New topic
Author

Calling Superclass version of an Overridden Method

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
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

Joined: Jan 26, 2007
Posts: 160
How will you do it using reflection
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

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?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
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

Joined: Sep 29, 2008
Posts: 12617

Alec Lee wrote:So, this can be done by reflection?

Yes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling Superclass version of an Overridden Method
 
Similar Threads
Dan's Thread question...
Doubt: equals() & hashcode() method
Doubt with instance variables and methods of the subclass and superclass
Mechanism of init method to initialize servlet
About the finalize() method