All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.
Consider the following code:
public class MyClass {
protected int value = 10;
}
Which of the following statements are correct regarding the field value?
It can be read and modified from any class within the same package or from any subclass of MyClass.
The answer is partially correct because you can read and modify if you are in the same package.
However if there is a class in another package which extends MyClass it can only inherit the variable, but it can read or modify the value of a variable of a MyClass instance.
It is true that a subclass in another package cannot modify the field of an instance of class MyClass but as you mentioned the field is actually inherited and that can be accessed from the subclass. For example:
package x;
public class A{
protected int val = 10;
}
package y;
import x.*;
public class AA extends A{
public static void main(String[] args)
{
AA aa = new AA();
System.out.println(aa.val); //this works
}
}
This is what the question is trying to imply. But you are right, the option can be ambiguous. It has now been improved.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.