Javaはオブジェクト指向プログラミング言語です。継承は最も重要な機能の1つです。オブジェクトの状態を非表示にするためにカプセル化を使用します。以下のプログラムでは、年齢とカウンターにプライベートアクセス修飾子を使用する必要がありましたが、この継承をテストするためにパブリックを使用しました。
- オブジェクトの状態に直接アクセスする場合(プリミティブ型またはオブジェクト参照)に継承が機能しない理由。SOP3の出力が予想と異なります。
- コンパイラはSOP13行とSOP14行でエラーを出さないため。なぜFatherクラスの詳細が子クラスではなく出力されるのか。
。
public class Father {
public int age = 50;
/*
* Counter keeps track of total no of instances created so far.
*/
public static int counter = 0;
public Father(){
super();
synchronized (Father.class) {
++Father.counter;
}
}
public int getAge(){
return this.age;
}
public static int getStaticCount(){
return Father.counter;
}
}
public class Child extends Father {
public int age = 25;
public static int counter = 0;
public Child(){
super();
synchronized (Child.class) {
++Child.counter;
}
}
public int getAge(){
return this.age;
}
public static int getStaticCount(){
return Child.counter;
}
public static void main(String args[]){
Father father = new Father();
Father child = new Child();
Child realChild = new Child();
System.out.println("Expecting Father Class details to be printed");
System.out.println("SOP 1 : Father Age : "+father.age); //prints 50 as expected.
System.out.println("SOP 2 : Father Age : "+father.getAge());//prints 50 as expected.
System.out.println("Expecting Child Class details to be printed");
/*
* Why inheritance does not work in case of direct integer access.
*/
System.out.println("SOP 3 : Child Age : "+child.age); //prints 50 ?? , Father Age . Why ?
System.out.println("SOP 4 : Child Age : "+child.getAge());//prints 25 as expected.
System.out.println("Expecting Child Class details to be printed");
System.out.println("SOP 5 : Child Age : "+realChild.age); //prints 25 as expected.
System.out.println("SOP 6 : Child Age : "+realChild.getAge());//prints 25 as expected.
/*
*Total No of static Count : proper way of accessing static field using Class Name.
*/
System.out.println("SOP 7 : Father Instance Count : Using Class Reference :"+Father.counter);
System.out.println("SOP 8 : Father Instance Count : Using Class Reference :"+Father.getStaticCount());
/*
* Incorrect Way to use static. Since Compiler allows this lets see output.
*/
System.out.println("SOP 9 : Father Instance Count : Using Object Reference :"+father.counter); //prints 3 as expected.
System.out.println("SOP 10 : Father Instance Count : Using Object Reference :"+father.getStaticCount());//prints 3 as expected.
/*
*Total No of static Count : proper way of accessing static field using Class Name.
*/
System.out.println("SOP 11 : Child Instance Count : Using Class Reference :"+Child.counter); // output is 2 as expected
System.out.println("SOP 12 : Child Instance Count : Using Class Reference :"+Child.getStaticCount()); // output is 2 as expected
/*
* Incorrect Way to use static.Since Compiler allows this lets see output.
* This invokes function of parent class. Why ? Inheritance does not work for static fields.
*/
System.out.println("SOP 13 : child Instance Count : Using Object Reference :"+child.counter); // output is 3 but expected is 2 . why ?
System.out.println("SOP 14 : child Instance Count : Using Object Reference :"+child.getStaticCount()); // output is 3 but expected is 2 . why ?
/*
* Incorrect Way to use static.Since Compiler allows this lets see output.
* This invokes function of parent class. Why ?
*/
System.out.println("SOP 15 : child Instance Count : Using Object Reference :"+realChild.counter); // output is 2 as expected
System.out.println("SOP 16 : child Instance Count : Using Object Reference :"+realChild.getStaticCount()); // output is 2 as expected
}
}
私の質問は、継承がインスタンスメソッドに対してのみ機能する理由です。SOP 3、SOP 13、SOP14で出力が異なる理由。