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 ...