An interface with different method parameters

45,586

Solution 1

you could make an operand-object

public interface Operation {

public int Add(Operand o);

}

or

public interface Operation {

 public int Add(Operand... o);

}

Solution 2

A few points worth mentioning regarding the other answers:

My suggestion would be to implement something like the Command pattern. To decouple the command implementation from its implementations is precisely its intent.

Solution 3

in this case you are not using the same interface, but you can create an hereditary system in which some parameters are automatically inserted:

public class CLASSA{
     public int Add(int id,String name){
         //something
     }
}

public class CLASSB extends CLASSA{
     public int Add(String name){
         super.Add(this.FIXED_ID, name);
         //something
     }
}

public class OPERATION extends CLASSA{
     public int Add(){
         super.Add(this.FIXED_ID, this.FIXED_NAME);
         //something
     }
}

Solution 4

A better approach would be to declare the parameter as a field in the class. Now pass it as a parameter to the constructor, so now it is specified when a new instance of the class is created.

Solution 5

Two solutions
Either define two methods in the interface:

public interface Operation {

public int addName(String name);
public int addIdName(int id, String name);

}

or use a generic paramter

public interface Operation {

    public int add(HashMap<String,String> keyValueParams);
}

But for Operations this should work different.

Share:
45,586
user1874800
Author by

user1874800

Updated on July 09, 2022

Comments

  • user1874800
    user1874800 almost 2 years

    I want to declare an interface be use with several classes
    this classes have method with different parameters

    interface:

    public interface Operation {
    
    public int Add();
    
    }
    

    class A:

    public class CLASSA implement Operation{
    
         public int Add(int id,String name);
    
    }
    

    class B:

    public class CLASSB implement Operation{
    
         public int Add(String name);
    
    }
    

    how to impelement of this interface?

  • El Hocko
    El Hocko about 11 years
    now you can use this interface and simulate different arguments there is no other way to do this, because otherwise, you would need different interfaces
  • El Hocko
    El Hocko about 11 years
    but now, your interface us useless, because Add(int,String) is not defined in here... also, java-convention tells you to use lowercase method names add()
  • Aram Kocharyan
    Aram Kocharyan about 11 years
    @cIph3r you're absolutely right, hadn't noticed. OP should also not use full caps on the class name.
  • Aram Kocharyan
    Aram Kocharyan about 11 years
    This gives flexibility but needs extra type checking, but this may be a necessary tradeoff in some situations so it's a useful solution imo. It also means you could subclass Operand and add logic for this subclass in an implementation of the interface, allowing any type of arguments to be passed. Note however that Operands should read Operand if that is indeed the name of the class.
  • Aram Kocharyan
    Aram Kocharyan about 11 years
    Note however that it's generally better to add extra details within subclasses and not take them away, since any modifications will result in less maintenance required.
  • kolboc
    kolboc almost 5 years
    link expired but quick google for command pattern should be enough
  • Immanuel
    Immanuel over 3 years
    wikipedia for the lazy people: Command Pattern