Protected access modifier in Java

后端 未结 4 1962
温柔的废话
温柔的废话 2020-11-30 07:56

I am having a little trouble understanding the protected access modifier in java (or the design behind it). I thought it meant package access and access through objects that

相关标签:
4条回答
  • 2020-11-30 08:33

    The webpage @MadProgrammer linked gives a decent explanation:

    "The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package."

    This means the protected member must be accessed directly through either the class it is defined in or a subclass of said class while also being within the appropriate package. It does not necessarily mean you can access the protected member through an instance of said class created within a subclass of said class. The focus is on the packages involved.

    Here are your examples:

    package first; // Current package
    
    First fst = new First(); // from package first and does not extend anything
    fst.pro();
    

    Attempting to access member in question from which package? first

    Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes, First is defined in package first, so the protected member is accessible from First in package first.

    package second; // Current package
    
    First fst = new First(); // from package first and does not extend anything
    fst.pro();
    

    Attempting to access member in question from which package? second

    Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? No, First is defined in package first, so protected makes the member inaccessible from First in package second.

    package second; // Current package
    
    Second sec = new Second(); // from package second and extends First from package first
    sec.pro();
    

    Attempting to access member in question from which package? second

    Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes, Second, which is defined in package second, inherits the member from First, so the protected member is accessible from Second in package second.

    More examples for clarity:

    package first; // Current package
    
    Second sec = new Second(); // from package second and extends First from package first
    sec.pro();
    

    Attempting to access member in question from which package? first

    Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes, Second inherits the member from First, which is defined in package first, so the protected member is accessible from Second in package first.

    package first; // Current package
    
    Third third = new Third(); // from package third and extends Second from package second,
                               // which extends First from package first
    third.pro();
    

    Attempting to access member in question from which package? first

    Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes, Third inherits the member from Second, which inherits it from First where the member is defined (package first), so the protected member is accessible from Third in package first.

    0 讨论(0)
  • 2020-11-30 08:49

    protected means visibile to other classes within the same package. since Second is in a different package 'second.Second', it cannot access .pro() in first.

    if you created Second under the package name of 'first' - it would then work.

    0 讨论(0)
  • 2020-11-30 08:50

    Protected member in java

    Same package-(it behaves like default)

    It is accessible with in the sameclass,its child classes and also non child classes.(it behaves like default) You can even access protected members using parent or child reference.

    Outside package-

    It is accessible only to itz child class and the most important point here is that reference used must be of the same child class only

    0 讨论(0)
  • 2020-11-30 08:55

    protected vs. defaultHere is a diagram to show the access level. You code belongs to the R(Reference) case in Convertible in the diagram. That is, reference in subclass in another package is not allowed to access.

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