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 java reflection to invoke parent class method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java reflection to invoke parent class method" Watch "java reflection to invoke parent class method" New topic
Author

java reflection to invoke parent class method

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
In the parent class, I have a public method, then in the child class, I try to access it using java reflection, it doesn't work.

How to work it out ?

Thanks
Mehar Simhadri
Greenhorn

Joined: Jan 25, 2009
Posts: 4
Hi
I am able to access the parent methods also in the child using reflection

Can you elaborate more on what are you trying to do...

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41147
    
  45
Reflection is definitely not a beginner's subject, so I'll move the discussion to a more appropriate forum. Also, please note that ItDoesntWorkIsUseless. If you told us what you tried, and what exactly did or did not happen, we'd be in a much better position to help.


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Which method are you using to get the method?

getDeclaredXXX only returns members actually declared in that class; public methods and fields of the parent class are not found.

getXXX only returns public members in that class, also those declared in the parent class (except when overridden / hidden). Non-public members are not found.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
I wrote it's now... don't know if its works.



Each of their nuggets of wisdom contracted to a sound bite: Joshua Bloch: Write Lots of Code; Chet Haase: Don't Put Your Entire Application in One Method; Masood Mortazavi: Start Simple and Keep Learning; Cay Horstmann: First, Don't Panic
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
I wrote it's now... don't know if its works.



And if you want access and invoke the private Methods you only need add this line before "return m;"
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Marky Vasconcellos wrote:I wrote it's now... don't know if its works.


That solution would work for all methods, not only public ones. There is one "but" though: getMethod and getDeclaredMethod never return null, but throw a NoSuchMethodException instead. So your code would be this:

Of course you can wrap the NoSuchFieldException in a RuntimeException if you don't want the checked exception:

Just be careful - your method only looks for methods without a parameter.


Now, as I've said before, if you only need to look for public methods, it's much simpler:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: java reflection to invoke parent class method
 
Similar Threads
Java reflection get Method Body
Accessing methods with primitive argument via reflection
Use of reflection API
run time error
Real Usage of java Reflection