Java: cannot access protected member of superclass in expanding subclass

I need discussions about this, but I could not bear the answer to my question. Still need help.

Here is my code:

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 } } 

Some definitions after which the above example seems confusing:

  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. 

Question: Why can't I access the protected member ( int variable = 5 ) from a subclass of YetAnotherClass instance ( bill object)?

+6
source share
5 answers

Classes in other packages that are subclasses of the declaration class can only access their own inherited protected members.

 public class ATypeNameProgram extends YetAnotherClass{ public ATypeNameProgram() { System.out.println(this.variable); // this.variable is visible } } 

... but not inherited elements of protected objects.

 public class ATypeNameProgram extends YetAnotherClass{ public ATypeNameProgram() { System.out.println(this.variable); // this.variable is visible } public boolean equals(ATypeNameProgram other) { return this.variable == other.variable; // error: YetAnotherClass.variable is not visible } } 
+3
source

bill is not part of the YetAnotherClass subclass. The bill is a separate YetAnotherClass.

Try int bill = this.variable; (inside the constructor) to access the elements of the subclass.

+1
source

Your code will work if YetAnotherClass is in the same package as ATypeNameProgram . Like others, this will not work in other cases. Here is a working example.

 package my.example; public class MainClass extends MyAnotherClass { public static void main(String[] args) { MyAnotherClass bill = new MyAnotherClass(); System.out.println(bill.value); // this will work } } package my.example; public class MyAnotherClass { protected int value = 5; } 
+1
source

The Foo class can only access protected instances of an instance of type Bar if and only if Bar can be assigned to Foo . Ie if we can write:

 Foo foo = new Bar(); 

For example, let's say that we have:

 package a; public class Base { protected int protectedField; } 

Then we can have this:

 package b; import a.Base; public class Parent extends Base { void foo() { int i = this.protectedField; } void foo(Parent p) { int i = p.protectedField; } void foo(Child c) { int i = c.protectedField; } } class Child extends Parent { } 

This will be compiled because all protectedField are accessible through Parent instances. Note that since the Parent reference can be an instance of Child (i.e., we can write Parent p = new Child(); ), we can access c.protectedField .

The following commands will not compile:

 package b; import a.Base; public class Parent extends Base { void foo(Stepchild sc) { int i = sc.protectedField; // ERROR } } class Stepchild extends Base {} 

because the Stepchild instance Stepchild not an instance of Parent .

Somewhat vaguely, this also does not compile:

 package b; import a.Base; public class Parent extends Base {} class Child extends Parent { void foo(Parent p) { p.protectedField; // ERROR } } 

this is because the Parent object is not a superclass or superinterface of the Child , therefore the Child cannot access its protected members.

If you are having trouble remembering, just think about whether you can write the type to the class type reference. For example, we can write:

 Parent p = new Child(); 

but can't write

 Child c = new Parent(); // ERROR Parent p = new Stepchild(); // ERROR 

therefore, Child will not have access to the Parent protected members, and Parent will not have access to the Stepchild protected members.

Endpoint pair:

Remember that access to protected provides visibility between packages. In my experience, people forget about it.

Finally, protected static elements are always visible among the inheritance hierarchy.

+1
source

You do not instantiate a class that extends it, but a parent class. Check out the code below:

 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 ATypeNameProgram a = new ATypeNameProgram(); System.out.println(a.variable); //this will work } } 
0
source

Source: https://habr.com/ru/post/947074/


All Articles