Protected Visibility

suggest change

Protected visibility causes means that this member is visible to its package, along with any of its subclasses.

As an example:

package com.stackexchange.docs;
public class MyClass{
    protected int variable; //This is the variable that we are trying to access
    public MyClass(){
        variable = 2;
    };
}

Now we’ll extend this class and try to access one of its protected members.

package some.other.pack;
import com.stackexchange.docs.MyClass;
public class SubClass extends MyClass{
    public SubClass(){
        super();
        System.out.println(super.variable);
    }
}

You would be also able to access a protected member without extending it if you are accessing it from the same package.

Note that this modifier only works on members of a class, not on the class itself.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Visibility controlling access to members of a class:
* Protected Visibility

Table Of Contents
8 Arrays
10 Maps
11 Strings
19 Visibility controlling access to members of a class
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap