Prerrequisitos: Interfaces en Java , Java y Herencia Múltiple
Una clase puede extender otra clase y/o puede implementar una y más de una interfaz.
// Java program to demonstrate that a class can // implement multiple interfaces import java.io.*; interface intfA { void m1(); } interface intfB { void m2(); } // class implements both interfaces // and provides implementation to the method. class sample implements intfA, intfB { @Override public void m1() { System.out.println("Welcome: inside the method m1"); } @Override public void m2() { System.out.println("Welcome: inside the method m2"); } } class GFG { public static void main (String[] args) { sample ob1 = new sample(); // calling the method implemented // within the class. ob1.m1(); ob1.m2(); } }
Producción;
Welcome: inside the method m1 Welcome: inside the method m2
Herencia de interfaz: una interfaz puede extender otra interfaz.
// Java program to demonstrate inheritance in // interfaces. import java.io.*; interface intfA { void geekName(); } interface intfB extends intfA { void geekInstitute(); } // class implements both interfaces and provides // implementation to the method. class sample implements intfB { @Override public void geekName() { System.out.println("Rohit"); } @Override public void geekInstitute() { System.out.println("JIIT"); } public static void main (String[] args) { sample ob1 = new sample(); // calling the method implemented // within the class. ob1.geekName(); ob1.geekInstitute(); } }
Producción:
Rohit JIIT
Una interfaz también puede extender múltiples interfaces.
// Java program to demonstrate multiple inheritance // in interfaces import java.io.*; interface intfA { void geekName(); } interface intfB { void geekInstitute(); } interface intfC extends intfA, intfB { void geekBranch(); } // class implements both interfaces and provides // implementation to the method. class sample implements intfC { public void geekName() { System.out.println("Rohit"); } public void geekInstitute() { System.out.println("JIIT"); } public void geekBranch() { System.out.println("CSE"); } public static void main (String[] args) { sample ob1 = new sample(); // calling the method implemented // within the class. ob1.geekName(); ob1.geekInstitute(); ob1.geekBranch(); } }
Producción:
Rohit JIIT CSE
¿Por qué la herencia múltiple no se admite a través de una clase en Java, pero puede ser posible a través de la interfaz?
La herencia múltiple no es compatible con la clase debido a la ambigüedad. En el caso de la interfaz, no hay ambigüedad porque la implementación de los métodos la proporciona la clase de implementación hasta Java 7. A partir de Java 8, las interfaces también tienen implementaciones de métodos. Entonces, si una clase implementa dos o más interfaces que tienen la misma firma de método con implementación, también se le exige implementar el método en la clase. Consulte Java y herencia múltiple para obtener más detalles.
Este artículo es una contribución de Nitsdheerendra . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@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