Dos interfaces con los mismos métodos que tienen la misma firma pero diferentes tipos de devolución

Java no admite herencias múltiples, pero podemos lograr el efecto de herencias múltiples usando interfaces. En las interfaces, una clase puede implementar más de una interfaz, lo que no se puede hacer mediante la palabra clave extends.
Consulte Herencia múltiple en Java para obtener más información.
Digamos que tenemos dos interfaces con el mismo nombre de método (geek) y diferentes tipos de devolución (int y String)

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

Ahora, supongamos que tenemos una clase que implementa ambas interfaces:

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

La pregunta es: ¿Puede una clase Java implementar dos interfaces con los mismos métodos que tienen la misma firma pero diferentes tipos de devolución?
No, es un error.
Si dos interfaces contienen un método con la misma firma pero diferentes tipos de devolución, entonces es imposible implementar ambas interfaces simultáneamente.
De acuerdo con JLS (§8.4.2) , los métodos con la misma firma no están permitidos en este caso.

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.

Es un error de tiempo de compilación declarar dos métodos con firmas equivalentes a anular en una clase.

// 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[])
    {
    }
}

Producción:

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
// 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";
    }
}
error: MainClass is not abstract and does not override abstract method fun() in InterfaceA

Este artículo es una contribución de Bishal Kumar Dubey . Si le gusta GeeksforGeeks y le gustaría contribuir, también puede escribir un artículo usando contribuya.geeksforgeeks.org o envíe su artículo por correo a contribuya@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.

Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *