Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Protected field not accessible from the subclass? [duplicate]

Tags:

java

protected

I am in process of learning the Java access modifiers. For that, I have created a class Machine:

package udemy.beginner.interfaces;

public class Machine {

    public String name;
    private int id;
    protected String description;
    String serialNumber;

    public static int count;

    public Machine(){
        name = "Machine";
        count++;
        description = "Hello";
    }

}

Then, in another package, I have created a class Robot as a subclass of a car Machine:

package udemy.beginner.inheritance;

import udemy.beginner.interfaces.Machine;

public class Robot extends Machine {

    public Robot(){

        Machine mach1 = new Machine();
        String name = mach1.name;
        //here I am getting error "The field Machine.description is not visible" 
        String description = mach1.description; 
    }

}

I am getting an error when trying to access the field description in the class Robot. From my understand of how protected access modifier works, it should be OK though, but maybe I messed up something. Any thoughts?


EDIT: I have tried to move Robot class to the same package as Machine class is in and now it works, without a need to use this. If someone can explain me this. According to the answers below, it should not work as well ...

like image 703
MichalB Avatar asked Dec 24 '22 22:12

MichalB


2 Answers

You can't access a protected superclass field in a different instance of the class.

There's a good reason: you don't know whether it has the same subclass as yourself, or a completely different subclass. If it were allowed to access the protected field, you would be allowed to access the internals of entirely unrelated classes.

If you are sure that the object is of the same subclass as the class that wants to access the superclass field, you can cast the object; when you that, you can access the protected field.

The rules are described in the Java Language Specification section 6.6.2

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.

6.6.2.1. Access to a protected Member

Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C.

In addition, if Id denotes an instance field or instance method, then:

  • If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S. [This is the relevant section]
like image 60
Erwin Bolwidt Avatar answered Apr 27 '23 19:04

Erwin Bolwidt


protected variables are accessible outside class, but only through inheritance. So, if you change that statement to this:

public Robot(){

    Machine mach1 = new Machine();
    String name = mach1.name;
    // This will work (access on `this` reference)
    String description = this.description; 
}

Actually protected modifier means that, the field is visible to be inherited by the subclasses, and it can be used only there, using this reference.

like image 41
Rohit Jain Avatar answered Apr 27 '23 20:04

Rohit Jain