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

前端 未结 4 1505
梦谈多话
梦谈多话 2020-11-29 05:55

I have an abstract class, relation in package database.relation and a subclass of it, Join, in package database.operations

相关标签:
4条回答
  • 2020-11-29 06:28

    A little known caveat about protected:

    6.6.2 Details on protected Access

    A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

    0 讨论(0)
  • 2020-11-29 06:28

    If protected, your instance of Join cannot access the mStructure in other instances (relRight, relLeft) outside the package.

    EDIT:

    The table here explains this situation fairly well. I marked the culprit in your question with []s

    Access Levels
    Modifier    Class Package Subclass  World
    public      Y     Y       Y         Y
    protected   Y    [Y]      Y         N
    no modifier Y     Y       N         N
    private     Y     N       N         N
    
    0 讨论(0)
  • 2020-11-29 06:29

    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.

    0 讨论(0)
  • 2020-11-29 06:35

    The problem is that you are accessing other instance protected member.

    You can apply multiple solutions, for example if possible you can declare in the parent class these two methods:

    protected void copyRelationStructure(Relation r) {
      this.copyStructure(r.mStructure);
    }
    
    protected void mergeRelationStructure(Relation r) {
      for (final Header header: r.mStructure) {
        if (!mStructure.contains(header)) {
          mStructure.add(header);
        }
      }
    }
    

    And then in childs code replace:

    this.copyStructure(mRelLeft.mStructure);
    
    for (final Header header :mRelRight.mStructure) {
      if (!mStructure.contains(header)) {
        mStructure.add(header);
      }
    }
    

    With:

    this.copyRelationStructure(mRelLeft);
    this.mergeRelationStructure(mRelRight);
    

    That should work. Now Relation has the responsibility to provide methods that allow operations with itself inners to its children. Probably the reason behind this policy is that children should not mess with parent's internals unless they are part of the same software bundle in order to limit incompatibilities.

    0 讨论(0)
提交回复
热议问题