在本文中,我们将了解如何实现多重继承。 java不支持多重继承。这意味着一个类不能扩展多个类,但我们仍然可以使用关键字“extends”来实现结果。
算法step 1 – startstep 2 – declare three classes namely server, connection and my_teststep 3 – relate the classes with each other using 'extends' keywordstep-4 – call the objects of each class from a main function.step 5 – stop
example 1的中文翻译为:示例 1class 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 successfullyconnection to backend established successfullyconnection to frontend established successfully
示例 2interface 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 foodi am trying this route
以上就是java程序实现多重继承的详细内容。