English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
在此示例中,我们将学习在Java中实现多重继承。
要理解此示例,您应该了解以下Java编程主题:
当子类从多个超类继承时,称为多重继承。但是,Java不支持多重继承。
要在Java中实现多重继承,我们必须使用接口。
interface Backend { //抽象类 public void connectServer(); } class Frontend { public void responsive(String str) { System.out.println(str + " 也可以用作前端."); } } // Language 继承了 Frontend 类 // Language 实现了 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) { // Create an object of the Language class Language java = new Language(); java.connectServer(); //Call 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.
In this case, the Language class inherits the properties of Backend and Frontend. Therefore, it can be said that this is an instance of multiple inheritance.