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?
Overloading methods of an interfaceYes, you can have overloaded methods (methods with the same name different parameters) in an interface. You can implement this interface and achieve method overloading through its methods.
Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class. An interface can contain any number of methods.
The interface body can contain abstract methods, default methods, and static methods. An abstract method within an interface is followed by a semicolon, but no braces (an abstract method does not contain an implementation).
At present, a Java interface can have up to six different types. Interfaces cannot be instantiated, but rather are implemented. A class that implements an interface must implement all of the non-default methods described in the interface, or be an abstract class.
you could make an operand-object
public interface Operation {
public int Add(Operand o);
}
or
public interface Operation {
public int Add(Operand... o);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With