astralmaster
astralmaster

Reputation: 2465

Pass different classes as parameters to a function Java

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?

Upvotes: 0

Views: 4353

Answers (5)

iTech
iTech

Reputation: 18430

Your first code example is incorrect because c is of type Class which does not have DoStuff()

Your second code is more appropriate, just you need to cast your object to a type that have DoStuff() method (e.g. an interface that all your classes implement)

Alternatively, you can just call the method DoSomething() with reflection

e.g.,

Method method = c.getMethod("DoSomething");
Object obj = c.newInstance();
method.invoke(obj);

Upvotes: 1

11684
11684

Reputation: 7507

I think you are confusing the type Class with the type Object. The class Object is directly or indirectly the superclass of all the classes you will ever create in Java (with the currently available versions of Java at least). Because every object extends the class Object you can pass every object to a method that expects a parameter of the type Object. The type Class is something entirely different. An object of the type Class (I almost confused my self there) is obtained by a class literal (myInstance.class) or reflection. I suspect you passed an instance of a class to DoStuff(), and hence the compiler complained it was not an object of the type Class. Change Class to Object and you'll be fine, except that you have to cast that object to a class that implements DoSomething

Upvotes: 0

Hunter McMillen
Hunter McMillen

Reputation: 61515

I think what you really mean is that you are passing an instance of an Object to your doStuff() method, this Object then has a doSomething() method that you should be able to call. Unless you have an inheritance hierarchy, you would need to make your method take a parameter of type Object and cast to your specific Object.

public void doStuff(Object o) {
  if(o instanceof Class1) {
     Class1 c1 = (Class1)o;
     c1.doSomething();
  } 
  else if(o instanceof Class2) {
     Class2 c2 = (Class2)o;
     c2.doSomething();
  }
  else {
     ....
  }
}

Upvotes: -2

Erik
Erik

Reputation: 2051

Create an interface that declares your method doSomething like this:

   public interface Something {
      void doSomething();   
   }

and then have your class implement this interface like:

public class SomeClass implements Something {
    public void doSomething() {
        // do what?
    }
}

You can then do

public void DoStuff(Something pInstance) {
   pInstance.doSomething();
}

Upvotes: 3

Apurv
Apurv

Reputation: 3753

You can create an Interface that has a method doSomething() and you make all classes implement this Interface. You can then call doSomething() on all the classes.

Upvotes: 0

Related Questions