首页 > Java > java教程 > Java程序实现多重继承

Java程序实现多重继承

WBOY
发布: 2023-08-25 22:21:09
转载
818 人浏览过

Java程序实现多重继承

在本文中,我们将了解如何实现多重继承。 Java不支持多重继承。这意味着一个类不能扩展多个类,但我们仍然可以使用关键字“extends”来实现结果。

算法

Step 1 – START
Step 2 – Declare three classes namely Server, connection and my_test
Step 3 – Relate the classes with each other using 'extends' keyword
Step-4 – Call the objects of each class from a main function.
Step 5 – STOP
登录后复制

Example 1

的中文翻译为:

示例 1

class Server{
   void my_frontend(){
      System.out.println("Connection to frontend established successfully");}
   }
   class Java extends Server{
      void my_backend(){
         System.out.println("Connection to backend established successfully");
      }
   }
   class connection extends Java{
      void my_database(){
         System.out.println("Connection to database established successfully");
      }
   }
   public class my_test{
      public static void main(String args[]){
         connection my_connection=new connection();
         my_connection.my_database();
         my_connection.my_backend();
         my_connection.my_frontend();
   }
}
登录后复制

输出

Connection to database established successfully
Connection to backend established successfully
Connection to frontend established successfully
登录后复制

示例 2

interface My_restaurents {
   void eat();
}
interface My_journey {
   void travel();
}
class Holiday implements My_restaurents, My_journey {
   public void eat() {
      System.out.println("I am trying this food");
   }
   public void travel() {
      System.out.println("I am trying this route");
   }
}
public class My_trip {
   public static void main(String args[]) {
      Holiday my_schedule = new Holiday();
      my_schedule.eat();
      my_schedule.travel();
   }
}
登录后复制

输出

I am trying this food
I am trying this route
登录后复制

以上是Java程序实现多重继承的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板