I have several classes that all have DoSomething() method within them. And a separate function that expects a Class as a parameter:
public void DoStuff(Class c) {
c.DoSomething();
}
But this code throws error: DoSomething() is undefined for the type Class. I also tried instantiating the Class like this:
public void DoStuff(Class c) {
Object o;
try {
o = c.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And then calling the DoSomething() method, but i get the same error.
What’s the correct way to pass a class as a parameter?
Create an
interfacethat declares your methoddoSomethinglike this:and then have your class implement this interface like:
You can then do