Java
 Computer >> コンピューター >  >> プログラミング >> Java

多重継承を実装する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

例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

  1. 正方形の領域を見つけるJavaプログラム

    この記事では、正方形の面積を見つける方法を理解します。正方形の面積は、次の式を使用して計算されます- side*side i.e. s2 以下は同じのデモンストレーションです- 正方形の辺がsの場合、正方形の面積はs 2で与えられます。 − 入力 入力が-であると仮定します Length of the side : 4 出力 必要な出力は-になります Area of the square : 16 アルゴリズム Step 1 - START Step 2 - Declare 2 integer values namely my_side and my_area. S

  2. Javaで数を数えるプログラムを実装するにはどうすればよいですか?

    プログラムはJLabelを使用します カウントラベルを保持するには、 JTextField 数値を保持するコンポーネントカウント 、 JButton 追加を作成するコンポーネント 、削除 およびリセット ボタン。追加ボタンをクリックすると、JTextFieldのカウントがインクリメントされます 投稿者 1 削除ボタンをクリックすると、カウントが「1」ずつ減らされます。 [リセット]ボタンをクリックすると、リセットされます 0へのカウント 。 例 import java.awt.*; import java.awt.event.*; import javax.swing.*; publ