Home > Java > javaTutorial > Java program implements multiple inheritance

Java program implements multiple inheritance

WBOY
Release: 2023-08-25 22:21:09
forward
818 people have browsed it

Java program implements multiple inheritance

In this article, we will learn how to implement multiple inheritance. Java does not support multiple inheritance. This means that a class cannot extend more than one class, but we can still use the keyword "extends" to achieve the result.

Algorithm

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
Copy after login

The Chinese translation of Example 1

is:

Example 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();
   }
}
Copy after login

Output

Connection to database established successfully
Connection to backend established successfully
Connection to frontend established successfully
Copy after login

Example 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();
   }
}
Copy after login

Output

I am trying this food
I am trying this route
Copy after login

The above is the detailed content of Java program implements multiple inheritance. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template