Adil
Adil

Reputation: 133

Java method which takes unknown parameter

Is there a way in java to write a method which will take an unknown object as a parameter? The objects will always have a common method which the method will then need to call. Here is an example:

public void aMethod (MultipleObjects object){

object.commonMethod();
// Do some stuff here
}

I'm not sure what this is called (if it exists) so its difficult to search on Google.

Upvotes: 2

Views: 4069

Answers (5)

CodeCamper
CodeCamper

Reputation: 6984

I understand a lot of people are interpreting your question to mean you want to know about interfaces but I am interpreting this "write a method which will take an unknown object as a parameter?" to mean how do I write a method to handle unknown objects. As the other answers already tell you unless they share a common interface you can't have them all call the same method. But in case you are asking for this(which is what I think your question is asking for) this is how you would custom handle different unknown parameters...

public void aMethod(Object... object) {
if(object==null)
{
    //whatever you want to do if no parameters are entered.
    return;
}
for (Object o : object) {
    if (o == null) {
    continue; //what to do if null entered
    }
    if (o instanceof Integer) {
    //whatever you want to do if it is an Integer
    }
    else if(o instanceof Double)
    {
    //whatever you want to do if it is a Double
    }
    else if(o instanceof Character)
    {
    //whatever you want to do if it is a Character
    }
    //and so on
 }
}

Upvotes: -1

Jason
Jason

Reputation: 11822

If you truly don't know what objects will be passed in and those object are not related through any kind of common base class or interface, then you will need to pass the object in as an Object reference and use reflection to find out if the object implements the method you want to call. If it does, then you again use reflection to call it.

Upvotes: 1

Chris Bogart
Chris Bogart

Reputation: 472

The usual way to do this is to define an interface that has just that method in it, then make sure all the classes that you might pass to aMethod implement that interface. E.g.:

interface CommonMethodHaver {
     void commonMethod();
}

class Class1 implements CommonMethodHaver {
     yadda yadda yadda;
     void commonMethod() {
         do class1-specific stuff here;
     }
}

...
public void aMethod(CommonMethodHaver cmh) {
    cmh.commonMethod();
    // Do some stuff here
}

Upvotes: 2

Christian Tapia
Christian Tapia

Reputation: 34166

You can make all those classes (which share the common method) to implement an interface, so you define the method like:

public void aMethod(SomeInterface obj) {
    obj.commonMethod();
    // ...
}

The interface would be:

public interface SomeInterface {
    public void commonMethod();
}

Upvotes: 2

arshajii
arshajii

Reputation: 129537

You need an interface:

interface MyInterface {

    void commonMethod();

}

class MyClass implements MyInterface {

    // implement `commonMethod()`

}

Now your method would be:

public void aMethod(MyInterface object) {
    ...
    object.commonMethod();
    ...
}

You can now pass an instance of MyClass (or any other class that implements MyInterface) to aMethod().

Upvotes: 2

Related Questions