If you "implement" an interface method, and change the parameter signature (like adding a third String), this no longer is a valid implementation of the interface. You have to have a method like
public Response onResp(String a, String b) {
// ...
}
in your MyClass
.
I need the additional String c in onResp method of MyClass
The interface IResp
most probably exists because there are callers of the onResp()
method, and they will provide two Strings, not three. If your onResp()
code really can't work without getting an additional piece of information (String c
), you have a problem.
Your proposals both won't work (if I understand them correctly):
- If you just add an empty
onResp(String a, String b)
method, then that method will be called from outside, do nothing and return.
- If you declare
MyClass
to be abstract, then you won't be able to create instances of MyClass
, and without an instance of this class, nothing will ever get called.
One (theoretical?) possibility is to change the interface method to use three instead of two parameters and to modify all callers of the interface to provide the expected information as a third parameter. But I guess the interface is fixed, and you're not free to do that.
If you can use the very same c
value in all onResp()
calls, you can provide the additional String c
not with every onResp()
call, but ahead of time, when you create the MyClass
instance, doing new MyClass("abc")
, assuming that "abc" is the appropriate value for c
. Your class should look like this:
public class MyClass implements IResp {
private String myConstantC;
public MyClass(String c) {
this.myConstantC = c;
}
privateResponse onResp(String a, String b, String c) {
// your code
}
@Override
public Response onResp(String a, String b) {
return onResp(a, b, myConstantC);
}
}
If you cannot live with a single constant c
value, but have a chance to somehow find out the necessary c
value for a given onResp(a, b)
call, you can do something like
public class MyClass implements IResp {
privateResponse onResp(String a, String b, String c) {
// your code
}
private String findTheCValue(String a, String b) {
// your way to find and return the correct c value
}
@Override
public Response onResp(String a, String b) {
String c = findTheCValue(a, b);
return onResp(a, b, c);
}
}