• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Reflection to call super class method

 
Kaushik Sathupadi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this helps you?



 
Kaushik Sathupadi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe something like the following could help? It will only need one IF statement for all your methods.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic