Java-inheritance
Java-継承
継承は、あるクラスが別のクラスのプロパティ(メソッドとフィールド)を取得するプロセスとして定義できます。 継承を使用すると、情報は階層的な順序で管理可能になります。
他のプロパティを継承するクラスはサブクラス(派生クラス、子クラス)と呼ばれ、プロパティが継承されるクラスはスーパークラス(ベースクラス、親クラス)と呼ばれます。
キーワードを拡張
*extends* は、クラスのプロパティを継承するために使用されるキーワードです。 extendsキーワードの構文は次のとおりです。
構文
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}
サンプルコード
以下は、Javaの継承を示す例です。 この例では、CalculationとMy_Calculationという2つのクラスを観察できます。
extendsキーワードを使用して、My_Calculationは、Calculationクラスのメソッドadd()およびSubtraction()を継承します。
次のプログラムをコピーして、My_Calculation.javaという名前のファイルに貼り付けます
- 例 *
class Calculation {
int z;
public void addition(int x, int y) {
z = x + y;
System.out.println("The sum of the given numbers:"+z);
}
public void Subtraction(int x, int y) {
z = x - y;
System.out.println("The difference between the given numbers:"+z);
}
}
public class My_Calculation extends Calculation {
public void multiplication(int x, int y) {
z = x* y;
System.out.println("The product of the given numbers:"+z);
}
public static void main(String args[]) {
int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
}
}
以下に示すように、上記のコードをコンパイルして実行します。
javac My_Calculation.java
java My_Calculation
プログラムを実行した後、それは次の結果を生成します-
出力
The sum of the given numbers:30
The difference between the given numbers:10
The product of the given numbers:200
指定されたプログラムでは、 My_Calculation クラスのオブジェクトが作成されると、スーパークラスのコンテンツのコピーがその中に作成されます。 そのため、サブクラスのオブジェクトを使用して、スーパークラスのメンバーにアクセスできます。
スーパークラス参照変数はサブクラスオブジェクトを保持できますが、その変数を使用するとスーパークラスのメンバーのみにアクセスできるため、両方のクラスのメンバーにアクセスするには、常にサブクラスへの参照変数を作成することをお勧めします。
上記のプログラムを検討する場合、以下に示すようにクラスをインスタンス化できます。 ただし、スーパークラス参照変数(この場合は cal )を使用すると、サブクラスMy_Calculationに属するメソッド* multiplication()*を呼び出すことはできません。
Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
注-サブクラスは、スーパークラスからすべてのメンバー(フィールド、メソッド、ネストされたクラス)を継承します。 コンストラクタはメンバではないので、それらはサブクラスによって継承されませんが、スーパークラスのコンストラクタはサブクラスから呼び出すことができます。
スーパーキーワード
*super* キーワードは *this* キーワードに似ています。 以下は、superキーワードが使用されるシナリオです。
- 同じ名前を持つ場合、サブクラスのメンバーからスーパークラスのメンバーを区別するために使用されます。
- サブクラスからスーパークラスコンストラクターを呼び出すために使用されます。
メンバーの差別化
クラスが別のクラスのプロパティを継承している場合。 また、スーパークラスのメンバーの名前がサブクラスと同じ場合、これらの変数を区別するために、以下に示すようにスーパーキーワードを使用します。
super.variable
super.method();
サンプルコード
このセクションでは、 super キーワードの使用法を示すプログラムを提供します。
指定されたプログラムには、_Sub_class_と_Super_class_という2つのクラスがあり、両方とも異なる実装のdisplay()という名前のメソッドと、異なる値のnumという変数を持っています。 両方のクラスのdisplay()メソッドを呼び出し、両方のクラスの変数numの値を出力しています。 ここで、スーパークラスのメンバーとサブクラスを区別するためにスーパーキーワードを使用したことがわかります。
Sub_class.javaという名前のファイルにプログラムをコピーして貼り付けます。
例
class Super_class {
int num = 20;
//display method of superclass
public void display() {
System.out.println("This is the display method of superclass");
}
}
public class Sub_class extends Super_class {
int num = 10;
//display method of sub class
public void display() {
System.out.println("This is the display method of subclass");
}
public void my_method() {
//Instantiating subclass
Sub_class sub = new Sub_class();
//Invoking the display() method of sub class
sub.display();
//Invoking the display() method of superclass
super.display();
//printing the value of variable num of subclass
System.out.println("value of the variable named num in sub class:"+ sub.num);
//printing the value of variable num of superclass
System.out.println("value of the variable named num in super class:"+ super.num);
}
public static void main(String args[]) {
Sub_class obj = new Sub_class();
obj.my_method();
}
}
次の構文を使用して、上記のコードをコンパイルおよび実行します。
javac Super_Demo
java Super
プログラムを実行すると、次の結果が得られます-
出力
This is the display method of subclass
This is the display method of superclass
value of the variable named num in sub class:10
value of the variable named num in super class:20
スーパークラスコンストラクターの呼び出し
クラスが別のクラスのプロパティを継承している場合、サブクラスはスーパークラスのデフォルトコンストラクターを自動的に取得します。 ただし、スーパークラスのパラメーター化されたコンストラクターを呼び出す場合は、以下に示すようにsuperキーワードを使用する必要があります。
super(values);
サンプルコード
このセクションで提供されるプログラムは、superキーワードを使用して、スーパークラスのパラメーター化されたコンストラクターを呼び出す方法を示しています。 このプログラムにはスーパークラスとサブクラスが含まれ、スーパークラスには整数値を受け入れるパラメーター化されたコンストラクターが含まれ、スーパーキーワードを使用してスーパークラスのパラメーター化されたコンストラクターを呼び出しました。
Subclass.javaという名前のファイルに次のプログラムをコピーして貼り付けます
例
class Superclass {
int age;
Superclass(int age) {
this.age = age;
}
public void getAge() {
System.out.println("The value of the variable named age in super class is: " +age);
}
}
public class Subclass extends Superclass {
Subclass(int age) {
super(age);
}
public static void main(String args[]) {
Subclass s = new Subclass(24);
s.getAge();
}
}
次の構文を使用して、上記のコードをコンパイルおよび実行します。
javac Subclass
java Subclass
プログラムを実行すると、次の結果が得られます-
- 出力 *
The value of the variable named age in super class is: 24
IS-A関係
IS-Aは、このオブジェクトの種類の1つです。* extends *キーワードを使用して継承を実現する方法を見てみましょう。
public class Animal {
}
public class Mammal extends Animal {
}
public class Reptile extends Animal {
}
public class Dog extends Mammal {
}
さて、上記の例に基づいて、オブジェクト指向の用語では、次のとおりです-
- 動物は哺乳類クラスのスーパークラスです。
- 動物は爬虫類クラスのスーパークラスです。
- 哺乳類と爬虫類は動物クラスのサブクラスです。
- 犬は、哺乳類と動物の両方のクラスのサブクラスです。
今、IS-A関係を考慮する場合、次のように言えます-
- 哺乳類IS-A動物
- 爬虫類IS-A動物
- 犬IS-A哺乳類
- したがって:犬IS-A動物も
extendsキーワードを使用すると、サブクラスは、スーパークラスのプライベートプロパティを除く、スーパークラスのすべてのプロパティを継承できます。
インスタンス演算子を使用することで、哺乳類が実際に動物であることを保証できます。
例
class Animal {
}
class Mammal extends Animal {
}
class Reptile extends Animal {
}
public class Dog extends Mammal {
public static void main(String args[]) {
Animal a = new Animal();
Mammal m = new Mammal();
Dog d = new Dog();
System.out.println(m instanceof Animal);
System.out.println(d instanceof Mammal);
System.out.println(d instanceof Animal);
}
}
これは、次の結果を生成します-
出力
true
true
true
*extends* キーワードについて十分に理解しているため、IS-A関係を取得するために *implements* キーワードがどのように使用されるかを見てみましょう。
一般に、 implements キーワードはクラスで使用され、インターフェイスのプロパティを継承します。 インターフェイスをクラスで拡張することはできません。
例
public interface Animal {
}
public class Mammal implements Animal {
}
public class Dog extends Mammal {
}
instanceofキーワード
*instanceof* 演算子を使用して、哺乳類が実際に動物であり、犬が実際に動物であるかどうかを確認します。
例
interface Animal{}
class Mammal implements Animal{}
public class Dog extends Mammal {
public static void main(String args[]) {
Mammal m = new Mammal();
Dog d = new Dog();
System.out.println(m instanceof Animal);
System.out.println(d instanceof Mammal);
System.out.println(d instanceof Animal);
}
}
これは、次の結果を生成します-
出力
true
true
true
HAS-A関係
これらの関係は、主に使用法に基づいています。 これは、特定のクラスが HAS-A 特定のものであるかどうかを判別します。 この関係は、コードの重複とバグの削減に役立ちます。
例を見てみましょう-
例
public class Vehicle{}
public class Speed{}
public class Van extends Vehicle {
private Speed sp;
}
これは、クラスVan HAS-A Speedを示しています。 Speedに個別のクラスを用意することで、speedに属するコード全体をVanクラスに入れる必要がなくなり、複数のアプリケーションでSpeedクラスを再利用できるようになります。
オブジェクト指向機能では、ユーザーはどのオブジェクトが実際の作業を行っているかを気にする必要はありません。 これを実現するために、Vanクラスは実装の詳細をVanクラスのユーザーから隠します。 したがって、基本的にユーザーはVanクラスに特定のアクションを実行するように要求し、Vanクラスは自分で作業を実行するか、別のクラスにアクションを実行するように要求します。
継承の種類
以下に示すように、継承にはさまざまなタイプがあります。
覚えておくべき非常に重要な事実は、Javaは多重継承をサポートしていないということです。 これは、クラスが複数のクラスを拡張できないことを意味します。 したがって、以下は違法です-
例
public class extends Animal, Mammal{}
ただし、クラスは1つまたは複数のインターフェイスを実装できます。これにより、Javaは多重継承の不可能性を取り除くことができました。