English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriais Básicos de Java

Controle de fluxo Java

Java Array

Java Orientação a Objetos (I)

Java Orientação a Objetos (II)

Java Orientação a Objetos (III)

Tratamento de Exceções Java

Java Lista (List)

Java Queue (Fila)

conjunto Java Map

conjunto Java Set

Java Entrada e Saída (I/O)

Reader Java/Writer

Outros tópicos Java

Implementação de multipla herança em programas Java

Java Examples

Neste exemplo, vamos aprender a implementar multipla herança em Java.

Para entender este exemplo, você deve entender o seguinteProgramação JavaTema:

Quando uma subclasse herda de múltiplos superclasse, é chamado de multipla herança. Mas, Java não suporta multipla herança.

Para implementar multipla herança em Java, devemos usar interfaces.

Exemplo: Multipla herança em Java

interface Backend {
  //classe abstrata
  public void connectServer();
}
class Frontend {
  public void responsive(String str) {
    System.out.println(str + Também pode ser usado como frontend.
  }
}
// Language herda classe Frontend
// Language implements a interface Backend
class Language extends Frontend implements Backend {
  String language = "Java";
  //方法实现接口
  public void connectServer() {
    System.out.println(language + "Can be used as a backend language.");
  }
  public static void main(String[] args) {
    // Creating an object of the Language class
    Language java = new Language();
    java.connectServer();
    //Calling the inherited method of the Frontend class
    java.responsive(java.language);
  }
}

Output Result

Java can also be used as a backend language.
Java can also be used as a frontend.

In the above example, we created an interface named Backend and a class named Frontend. The Language class inherits the Frontend class and implements the Backend interface.

Multiple Inheritance in Java

Here, the Language class inherits the properties of Backend and Frontend. Therefore, it can be said that this is an instance of multiple inheritance.

Java Examples