Two interfaces with same methods having same signature but different return types

Java does not support multiple inheritances but we can achieve the effect of multiple inheritances using interfaces. In interfaces, a class can implement more than one interface which can’t be done through extends keyword.
Please refer Multiple inheritance in java for more.
Let’s say we have two interfaces with same method name (geek) and different return types(int and String)

filter_none

edit
close

play_arrow

link
brightness_4
code

public interface InterfaceX
{
    public int geek();
}
public interface InterfaceY
{
    public String geek();
}

chevron_right


Now, Suppose we have a class that implements both those interfaces:

filter_none

edit
close

play_arrow

link
brightness_4
code

public class Testing implements InterfaceX, InterfaceY
{
public String geek()
    {
        return "hello";
    }
}

chevron_right


The question is: Can a java class implement Two interfaces with same methods having the same signature but different return types??
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.
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.

It is a compile-time error to declare two methods with override-equivalent signatures in a class.

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA program to illustrate the behavior
// of program when two interfaces having same 
// methods and different return-type
interface bishal
{
public
    void show();
}
  
interface geeks
{
public
    int show();
}
  
class Test implements bishal, geeks
{
    void show() // Overloaded method based on return type, Error
    {
    }
    int show() // Error
    {
        return 1;
    }
public
    static void main(String args[])
    {
    }
}

chevron_right


Output:

error: method show() is already defined in class Test
error: Test is not abstract and does not override abstract method show() in geeks
error: show() in Test cannot implement show() in bishal
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the behavior of the program
// when two interfaces having the same methods and different return-type
// and we defined the method in the child class
interface InterfaceA
{
public
    int fun();
}
interface InterfaceB
{
public
    String moreFun();
}
  
class MainClass implements InterfaceA, InterfaceB
{
public
    String getStuff()
    {
        return "one";
    }
}

chevron_right


error: MainClass is not abstract and does not override abstract method fun() in InterfaceA

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Akanksha_Rai



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.