Inheritance
Cedvelde gorduyumuz kimi varis klas ferqli paketlerin icinden yalniz public ve protected fieldleri gore biler. Eyni paketdedilerse o zaman default (package private) olanlarida gore biler.
package main.example3;
public class Parent {
public static int a = 1;
public int b = 2;
protected int c = 3;
private int d = 4;
int e = 5;
public static void method1() {
System.out.println("static method1 in Parent");
}
public void method2() {
System.out.println("public method2 in Parent");
}
protected void method3() {
System.out.println("protected method3 in Parent");
}
void method4() {
System.out.println("default method4 in Parent");
}
}
package main.example3;
public class Child extends Parent {
public static int a = 11;
public int b = 22;
protected int c = 33;
private int d = 44;
int e = 55;
public static void method1() {
System.out.println("static method1 in Child");
}
public void method2() {
System.out.println("public method2 in Child");
}
protected void method3() {
System.out.println("protected method3 in Child");
}
void method4() {
System.out.println("default method4 in Child");
}
}package main.example3;
public class Main {
public static void main(String[] args) {
Child child = new Child();
System.out.println(child.a);
System.out.println(child.b);
System.out.println(child.c);
System.out.println(child.e);
child.method1();
child.method2();
child.method3();
child.method4();
System.out.println("-------------------------------------");
Parent child1 = new Child();
System.out.println(child1.a); // variable hidden
System.out.println(child1.b); // variable hidden
System.out.println(child1.c); // variable hidden
System.out.println(child1.e); // variable hidden
child1.method1();
child1.method2();
child1.method3();
child1.method4();
}
}---------------------------------------------------------------------------------------------
*** Interesting cases ***
package main.example4;
public class Parent {
protected int i = 3;
protected boolean b = false;
protected static String s = "Parent";
public static void f() {
System.out.println("f() in Parent");
}
public void g() {
System.out.println("g() in Parent");
}
}package main.example4;
public class Child extends Parent {
private int i = 5;
private boolean b = true;
private static String s = "Child";
private static void f() {
System.out.println("f() in Child");
}
private void g() {
System.out.println("g() in Child");
}
}Niye Child klasda f() ve g() metodlarini private ede bilmirik? Eslinda cavab cox sadedir:
polimorfizme esasen biz : Parent parent = new Child(); yaza bilerik, bu zaman biz: parent.f()
dedikde ne baw verecek? Beli duzdur sistem cawacaq. Bu da sade cavab.
Bes yaxwi bu zaman bawqa sual ortaya cixir: f() metodunun access modifier-ini niye deyiwe
bilmirik, biz axu onsuzda parent.g() dedikde zaten bu Parentin icindeki metodu cagirir yeni
bu istisna hali idi axi?
Bu da cox sadedir: private etsek bu zaman bu metodu gore bilmeyeceyik, lakin bu ixtiyar niye
elimizden alinsinki duzdur? Menasiz seslenir. default etsek yeni package-private o zaman
problem aradan qalxmalidir duzdu? Cavab: bele bu polimorfizmi proqramci bawqa package-de
iwledecek. Yene menasiz seslenir. protected etsek yene eyni bawqa package anlayiwi ile qarwila
wiriq.
---------------------------------------------------------------------------------------------
*** Java-da static, private, final olan metodlar static olaraq baglanirlar.
Комментарии
Отправить комментарий