Seemingly simple things can lead to a lot of questions. In the process of learning, we just “seem to understand”, “seem to be so” and “should be ok” many concepts. In fact, what we lack is careful thinking and few “why” to ourselves.

In Java, access modifiers are the most basic. Protected modifiers are just one of them. Why not go public, default, and private? So after reading this article you’ll know why protected is worth thinking about more deeply ️.

In Thinking in Java, protected is called “inherited access,” which is what we remember as protected: Protected must be inherited to be accessible. So you think you understand, but do you really understand this sentence?

Consider a few questions:

  1. Can a subclass object access protected methods of its parent in the same package?

  2. Can a protected method of the parent class be accessed by creating that subclass object in a different package?

  3. Can I access protected methods of the parent class by creating an object in a subclass under different packages?

  4. Under different packages, can objects that create another subclass in a subclass access protected methods of the common parent class?

  5. What about the static modifier on the parent protected method?

There’s a line in Thinking in Java: “Protected also provides package access, that is, other classes within the same package can access protected elements.” The protected modifier contains access to the default modifier, so you already know the answer to question 1. In the same package, Either normal classes or subclasses can access the protected methods of the base class.

The parent class is a non-static protected modified class

package com.protectedaccess.parentpackage;

public class Parent {

    protected String protect = "protect field";

    protected void getMessage(a){
        System.out.println("i am parent"); }}Copy the code

Under different packages, protected methods in subclasses are not accessible by reference to their parent class

Either creating a Parent object or creating a Son1 object through polymorphism is not accessible as long as Parent references it and the compiler prompts an error.

package com.protectedaccess.parentpackage.sonpackage1;

import com.protectedaccess.parentpackage.Parent;

public class Son1 extends Parent{
    public static void main(String[] args) {
        Parent parent1 = new Parent();
        // parent1.getMessage(); error

        Parent parent2 = new Son1();
        // parent2.getMessage(); error}}Copy the code

Under different packages, its protected methods can be accessed in a subclass by reference to that subclass

The subclass actually inherits the method from the parent class, which can be accessed through the subclass object, directly from the subclass method, or by calling the method from the parent class using the super keyword.

package com.protectedaccess.parentpackage.sonpackage1;

import com.protectedaccess.parentpackage.Parent;

public class Son1 extends Parent{
    public static void main(String[] args) {
        Son1 son1 = new Son1();
        son1.getMessage(); // Output: I am parent,
    }
    private void message(a){
        getMessage();  // If a subclass overrides the method, output the contents of the override method
        super.getMessage(); // Outputs the contents of the method in the parent class}}Copy the code

Protected methods of the common base class cannot be accessed by another subclass reference in a subclass under different packages

package com.protectedaccess.parentpackage.sonpackage2;

import com.protectedaccess.parentpackage.Parent;

public class Son2 extends Parent {}Copy the code

Son2 is another subclass. Objects that create Son2 in Son1 do not have access to the protected methods of the parent class

package com.protectedaccess.parentpackage.sonpackage1;

import com.protectedaccess.parentpackage.Parent;
import com.protectedaccess.parentpackage.sonpackage2.Son2;

public class Son1 extends Parent{
    public static void main(String[] args) {
        Son2 son2 = new Son2();
        // son2.getMessage(); error}}Copy the code

The parent class is a static protected modified class

Protected static variables are directly accessible in subclasses, but not in non-subclasses of different packages

package com.protectedaccess.parentpackage;

public class Parent {

    protected String protect = "protect field";

    protected static void getMessage(a){
        System.out.println("i am parent"); }}Copy the code

Static methods are accessed directly by the class name

Whether the package is the same or not is directly accessible in subclasses

package com.protectedaccess.parentpackage.sonpackage1;

import com.protectedaccess.parentpackage.Parent;

public class Son3 extends Parent{
    public static void main(String[] args) {
        Parent.getMessage(); // Output: I am parent}}Copy the code

Under different packages, non-subclasses are not accessible

package com.protectedaccess.parentpackage.sonpackage1; import com.protectedaccess.parentpackage.Parent; public class Son4{ public static void main(String[] args) { // Parent.getMessage(); Error}}Copy the code

This should give you an idea of how many situations there are. There are different situations where unexpected results can occur, so you need to practice. You can’t really see the subtlety of the protected modifier just by reading about it in a book.