Chcę trochę dyskusji na ten temat, ale nie mogłem wywnioskować odpowiedzi na moją sprawę. Nadal potrzebujesz pomocy.Java: nie można uzyskać dostępu do chronionego członka nadklasy w rozszerzającej podklasie
Oto mój kod:
package JustRandomPackage;
public class YetAnotherClass{
protected int variable = 5;
}
package FirstChapter;
import JustRandomPackage.*;
public class ATypeNameProgram extends YetAnotherClass{
public static void main(String[] args) {
YetAnotherClass bill = new YetAnotherClass();
System.out.println(bill.variable); // error: YetAnotherClass.variable is not visible
}
}
Niektóre definicje, po której, w przykładzie powyżej wydaje się być mylące:
1. Subclass is a class that extends another class.
2. Class members declared as protected can be accessed from
the classes in the same package as well as classes in other packages
that are subclasses of the declaring class.
Pytanie: Dlaczego nie mogę Mam dostęp do chronionego elementu (int variable = 5
) z instancji podklasy YetAnotherClass
(bill
obiekt)?
Co to jest pytanie? –