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 Using Reflection to call super class 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 "Using Reflection to call super class method" Watch "Using Reflection to call super class method" New topic
Author

Using Reflection to call super class method

Kaushik Sathupadi
Greenhorn

Joined: Jun 23, 2006
Posts: 18
Assume that a Super class 'A' has, say, 20 methods. All of these are overriden in sub class 'B'. Now in a method in sub class 'B' i want to call one of the super class methods based on a parameter to this method. i don't want to put 20 if conditions to do this. Is there a way by which i can invoke the super class method using reflection. Note that this invocation should not call the overridern method in the sub class. Quite simply, is there a way to do "super.abcd()" using reflection.
If not, just out of curiosity, can i do this using JNI.
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

Is this helps you?





Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Kaushik Sathupadi
Greenhorn

Joined: Jun 23, 2006
Posts: 18
Hey thanks for answering prabhu. But my problem is that the methods are overriden in the sub class and i want to invoke the super class method. consider this example
Class Base{
public void test(){
System.out.println("test of base");
}
}

class Child extends Base{
public void test(){ //overriding super class method
super.test();// <- (1)
}
}
How do i replace line (1) using reflection, so that "test of base" is printed? (without creating a new instance of Base, ie., should be invoked on current instance)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I don't know if you can do it using reflection or JNI. But you can do it using the one line of code that you displayed with no trouble. And with zero if-conditions, not 20. (If you need to decide whether to call the super method or not, you have to execute the if's anyway.) So I would do that.
Kaushik Sathupadi
Greenhorn

Joined: Jun 23, 2006
Posts: 18
Hey paul,
I was just trying to give an example. In the actual scenario, I have many super class methods, all of which are overriden,(either in this class or in one of the sub classes). and i need to invoke one of the super class methods based on a input parameter to the method "test". consider this:

class Child extends Base{
public void test(String methodToCall){
if("test1".equals(methodToCall){
super.test1();
}
else if("test2".equals(methodToCall)){
super.test2();
}
..
..
..
else if("test100".equals(methodToCall)){
super.test100();
}
}
}

It would be a lot more easier if i can replace this redundant code with reflection.
Elle Atechsy
Ranch Hand

Joined: Jan 23, 2004
Posts: 96
Maybe something like the following could help? It will only need one IF statement for all your methods.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Reflection to call super class method