• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 145
  • Last Modified:

method execution on interfaces using reflection

I have a property defined which is a comma seperated list of classes.
Each one of these classes extend a Class A and implement methods of class B.
What I want to do is for each class in this list is to use the Class.forName(className) and
create a instance of this class using class.newInstance().

I know how to do this.
But how would I invoke the methods on class A and Class B.  Would I need to cast the object returned from class.newInstance() to A then invoke its method and then cast to the interface B and invoke its method

 ie  Object o = class.newInstance();
     A objectA = (A) o;
     objectA.doIt();
     B interfaceB = (B) o;
     interfaceB.method1();
     interfaceB.method2();
0
inzaghi
Asked:
inzaghi
  • 7
  • 5
  • 4
  • +1
3 Solutions
 
objectsCommented:
You need to either cast,
or you can use reflection to get the Method instance and exeute it.
0
 
CEHJCommented:
No - no need to cast to B
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
objectsCommented:
> no need to cast to B

yes you would to access the B methods
0
 
CEHJCommented:
>>yes you would to access the B methods

No - you shouldn't need to if A implements B
0
 
inzaghiAuthor Commented:
Class A does not implement B,
public class C extends A implements B{
}
0
 
CEHJCommented:
Now you're confusing me - C wasn't mentioned before ;-) In that case you don't need to cast C to B
0
 
inzaghiAuthor Commented:
Sorry for the confusion, just for clarity c is a actual class name that is defined in the comma seperated list of class names
0
 
objectsCommented:
Question seemed quite clear to me :)
0
 
inzaghiAuthor Commented:
To invoke the individual methods on the class A and B is it better to cast first or use reflection to get the methods and invoke?
0
 
CEHJCommented:
You'll get better performance by avoiding reflection where possible. This show why the cast is not necessary (if it's an accurate reflection of your hierarchy):

public class C extends A implements B {
      public void foo() {
            System.out.println("C.foo()");
      }

      public static void main(String[] args) {
            C c = new C();
            //((A)c).foo(); //not possible - (A doesn't implement foo)
            A a = new A();
            //c = (C)a; // not possible at run time
            ((B)c).foo(); // not necessary - the following will work just as well
            c.foo();
      }
}

interface B {
      public void foo();
}


class A {}
0
 
inzaghiAuthor Commented:
I will be creating Objects of the class C using:

Object object = Class.forName(className).newInstance();

Now how can I cast this object to its orginal type ie class C, would this do it

C objectC = (className.class) Class.forName(className).newInstance();
0
 
CEHJCommented:
Cast as you would normally:

C c =  (C)Class.forName(className).newInstance();
0
 
CEHJCommented:
You have a choice:

a. casting as above
b. using reflection to invoke the methods you want to invoke on the Object reference
0
 
objectsCommented:
> You have a choice:
> a. casting as above
> b. using reflection to invoke the methods you want to invoke on the Object reference

Which is exactly what I originaly said.

The example code you have in the q is the way to go.

0
 
aozarovCommented:
>> The example code you have in the q is the way to go.
Agree, though if "Each one of these classes extend a Class A and implement methods of class B." then why won't you either
make A as abstract class and let it implement B or as an alternative create a new abstract class which extends A and implements B and make
those classes extends this one instead of A. This way you can cast it to only one type.
0
 
CEHJCommented:
:-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now