Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't my subclass access a protected variable of its superclass, when it's in a different package?

I have an abstract class, relation in package database.relation and a subclass of it, Join, in package database.operations. relation has a protected member named mStructure.

In Join:

public Join(final Relation relLeft, final Relation relRight) {         super();         mRelLeft = relLeft;         mRelRight = relRight;         mStructure = new LinkedList<Header>();         this.copyStructure(mRelLeft.mStructure);          for (final Header header :mRelRight.mStructure) {         if (!mStructure.contains(header)) {             mStructure.add(header);         }     } } 

On lines

this.copyStructure(mRelLeft.mStructure); 

and

for (final Header header : mRelRight.mStructure) { 

I get the following error:

The field Relation.mStructure is not visible

If I put both classes in the same package, this works perfectly. Can anyone explain this issue?

like image 968
Amir Rachum Avatar asked Jun 18 '10 17:06

Amir Rachum


People also ask

Can I access the protected variable from a class in the different package?

Protected Access Modifier - Protected Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. The protected access modifier cannot be applied to class and interfaces.

Can a subclass access protected superclass?

protected allows you access from the same package, or parent classes. protected allows access within the same package, or by subclasses. Neither is the case when using a superclass object reference.

Can protected method be accessed by subclass in a different package?

Protected methods can only be accessible through inheritance in subclasses outside the package.

How do I access a protected method from another package?

The protected access modifier is accessible within the package. However, it can also accessible outside the package but through inheritance only. We can't assign protected to outer class and interface. If you make any constructor protected, you cannot create the instance of that class from outside the package.


1 Answers

It works, but only you the children tries to access it own variable, not variable of other instance ( even if it belongs to the same inheritance tree ).

See this sample code to understand it better:

//in Parent.java package parentpackage; public class Parent {     protected String parentVariable = "whatever";// define protected variable }  // in Children.java package childenpackage; import parentpackage.Parent;  class Children extends Parent {     Children(Parent withParent ){         System.out.println( this.parentVariable );// works well.         //System.out.print(withParent.parentVariable);// doesn't work     }  } 

If we try to compile using the withParent.parentVariable we've got:

Children.java:8: parentVariable has protected access in parentpackage.Parent     System.out.print(withParent.parentVariable); 

It is accessible, but only to its own variable.

like image 133
OscarRyz Avatar answered Sep 19 '22 13:09

OscarRyz