I had an interface initially as below.
public interface testMe {
public Set<String> doSomething();
}
public class A implements testMe {
public Set<String> doSomething() {
return // Set<String>
}
}
I had similar classes implementing testMe. Now I have to add one more class which returns Set<Some Object>
public class X implements testMe() {
public Set<Some OBject> doSomething() {
}
}
How could i add this method in the interface without breaking existing classes?
You can’t for two reasons.
A class or interface can’t have two or more methods that have the same number and type of parameters with the same name but differing return types; and
Because of type erasure, all
Set<...>instances are, at runtime, simplySet, so they would have the exact same return type anyway.You will need to name the second something different.
The more complicated answer is that you can make the parameter type extensible: