Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two interfaces specify methods with the same signature, but specified to have different behavior?

Tags:

java

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?

like image 410
AJMansfield Avatar asked Dec 05 '12 18:12

AJMansfield


People also ask

What happens when two interfaces have the same method?

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.

What happens if you implement two interfaces having the same name and the same signature for a default method?

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.

Is it possible to have two methods in a class with same method signature but different return types?

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.

CAN interface have multiple methods with same name?

C# allows the implementation of multiple interfaces with the same method name.

Can a class implement two interfaces with the same method signature?

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?

How to identify if two interfaces have the same method?

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.

What is the difference between an interface and a concrete class?

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.

Can two methods have the same signature in JLS?

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.


1 Answers

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.

like image 67
kosa Avatar answered Sep 27 '22 23:09

kosa