Class that implements two interfaces that have identical signature methods

Asked

Viewed 281 times

1

I have a class that implements two interfaces. Each of these interfaces has an identical method, for example:

    public interface InterfaceA{
       void doSomething(String params);
    }



    public interface InterfaceB{
       void doSomething(String params);
    }

Both have a method doSomething(String params) with the same name and signature. We know that every class that implements an interface must by contract, implement its methods or become abstract. Therefore, if I have a class that implements the two interfaces, the compiler will invoke the method of which interface when the ClasseQualquer is called? A ClasseQualquer implements the method of which interface? Since the methods in your signatures are identical it makes no difference?

public void ClasseQualquer implements InterfaceA, InterfaceB {
   @Override   
   public void doSomething(String params){

   }
}

2 answers

3


Your remark about contracts is correct, you have only one contract, but then it is not correct that there will be invocation of the method of one of the interfaces, there is no method in them, only contracts of methods (even has this from Java 8, but it is not a case that the question wants to know). The invocation will be the method that is in the class, that’s all.

The invocation can occur on any object that is of the class type, of its descending types (although it can be a descending method and not exactly of this class) or it can occur when using one of the two interfaces, which will call the class method with the same signing.

As Java has no explicit implementation of interface method it has no way to differentiate one or the other, so invoke by InterfaceA or by InterfaceB will call the same method. It has language, like C# for example, that allows each one to have its own implementation after the most likely is that you have very different goals between them although the method coincidentally has the same signature. Almost always if you have two interfaces with a method with the same signature and they have to do the same thing is error of design and Java cannot adequately handle the right cases where it has the same signature but different objectives.

So whether it makes a difference or not depends on what you’re doing. If the design is bad makes no difference, if in fact each method has different goal you will have to deal with it within the implementation of the method making the code less robust and slower.

Why would you make two interfaces with the same signature and goal? Or it is a hypothetical case that does not occur or is something poorly thought out (some cases that escape its control). Even the cases that the objectives are different you can think if there was a good choice there, it may make sense in some cases, but if it happens a lot it is also something badly architected.

  • Opa, it was just a hypothetical case. A doubt that came to me. It makes sense your answer and managed to cure my doubt. I will mark your reply as the correct answer to that post. Thank you very much!!!

2

When you say your class ClasseQualquer implements an interface, this is just a contract that exists during build time.

See on the other hand: implicit implementations. In some languages, you do not need to declare which class X implements Y interface, you only declare the Y interface, and if class X implements all the methods declared in the interface, then this class X is automatically implementing the Y interface.

Java has explicit implementations, you have to indicate that your class is actually implementing the interface, but this is just to ensure that you’re not implementing something by accident.

In the end, the important thing is that your class has the methods and attributes of that interface. If a function wants a parameter of the type InterfaceA, It just means she wants an object with the method public void doSomething(String). The fact that you have to declare your class by implementing the interface does not alter the behavior of compiled code, this is just a guarantee that the compiler requires.

  • Sensational friend. It further complemented the @Maniero user response and provided me with even more information about how the Java interface implementation works. Thank you very much!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.