I have some class public class myClass implements A, B
where A and B both contain a method public int doSomething();
, but A.doSomething
is specified by the interface to do something different than B.doSomething
.
I have read Two interfaces with same method signature implemented in Java class, but that doesn't really address my problem, because the methods are overriden to do the same thing, but as I said above, my question is about when they are specified in the interfaces to do different things.
For example, suppose A.doSomething()
is supposed to return 0
, while B.doSomething()
is supposed to throw an exception, and violating either one wold cause problems for methods that are supposed to take them as parameters.
Is there any way to do this in java? If so, how would one actually do it?
So, if the class already has the same method as an Interface, then the default method from the implemented Interface does not take effect. However, if two interfaces implement the same default method, then there is a conflict.
No, its an error If two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously.
The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return types. It will throw a compile-time error. If both methods have the same parameter types, but different return types, then it is not possible.
C# allows the implementation of multiple interfaces with the same method name.
Yes, a class can implement two two interfaces with the same method signature but that method should be implemented only once in the class. Implemented method. Implemented method. which package is always imported by default?
There is nothing to identify. Interfaces only proscribe a method name and signature. If both interfaces have a method of exactly the same name and signature, the implementing class can implement both interface methods with a single concrete method.
As in interface,we are just declaring methods,concrete class which implements these both interfaces understands is that there is only one method (as you described both have same name in return type). so there should not be an issue with it.You will be able to define that method in concrete class.
According to JLS (§8.4.2) methods with same signature is not allowed in this case. Two methods or constructors, M and N, have the same signature if they have, the same name the same type parameters (if any) (§8.4.4), and after adopting the formal parameter types of N to the type parameters of M, the same formal parameter types.
As per JLS (This is similar case like what you want, may not exact)
interface Fish { int getNumberOfScales(); }
interface StringBass { double getNumberOfScales(); }
class Bass implements Fish, StringBass {
// This declaration cannot be correct, no matter what type is used.
public ??? getNumberOfScales() { return 91; }
}
It is impossible to declare a method named getNumberOfScales
whose signature and return type are compatible with those of both the methods declared in interface Fish and in interface StringBass, because a class cannot have multiple methods with the same signature and different primitive return types (§8.4).
Unless you change your design by adding proxy (or) method signature it is not possible to do what you are expecting.
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