Sponsored Link •
|
Advertisement
|
Advertisement
|
This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.
Message:
> Note that I haven't written code for catching the execptions, instead I am throwing the exceptions to console. You should put your getMethod () and invoke () methods inside try blocks. One thing is important here is to declare m2 () as public because getMethod () method of class Class can get you only public method. If you don't want to declare m2() public then you can use getDeclaredMethod ( ) method of Class class which returns an array of Method object which includes all classes and then from array get your method reference and invoke the method. > // Import java.lang.reflect to use Method class > // A is your super class and B is sub class. I am calling method m2 on an object of class B from a method in class A. > void m1 ( A a) throws Exception { > // Get the class of the object > // Get the "m2" method reference for the object > // Get the method m2 ( ) > // Invoke the method m2 > > class B extends A { > public void m2 ( ) { > public class SuperSub { > public static void main ( String[] args ) throws Exception { > }
Replies: |
Sponsored Links
|