Junior — Middle
What type of access modifier ensures that a derived class can access the fields of the base class?
sobes.tech AI
Answer from AI
In Java, the access modifier protected allows a subclass to access the fields and methods of the superclass. Fields and methods with this modifier are accessible within the class itself, in its subclasses (even if they are in different packages), and also within classes of the same package.
Example:
class Parent {
protected int value = 10;
}
class Child extends Parent {
void printValue() {
System.out.println(value); // access to the protected field of the superclass
}
}