Page 1 of 1

About Question enthuware.ocajp.i.v7.2.856 :

Posted: Thu Nov 13, 2014 11:43 am
by aureobm
Question:

Code: Select all

Consider the following code:  
public class MyClass {    
    protected int value = 10;  
}  
Which of the following statements are correct regarding the field value?
Answer:

Code: Select all

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.

Re: About Question enthuware.ocajp.i.v7.2.856 :

Posted: Fri Nov 14, 2014 1:20 am
by admin
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:

Code: Select all

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.

Re: About Question enthuware.ocajp.i.v7.2.856 :

Posted: Mon Jan 25, 2016 7:42 am
by Deleted User 2655
I completely agree!

Re: About Question enthuware.ocajp.i.v7.2.856 :

Posted: Fri Mar 03, 2017 2:37 pm
by AndaRO
You must reformulate otherwise.

It's ambiguous.

Re: About Question enthuware.ocajp.i.v7.2.856 :

Posted: Fri Mar 03, 2017 2:44 pm
by AndaRO
It must be satisfied one condition:
subclass of class and can be in the same package or in different package.

Protected member can be only accessible only by inheritance.

Re: About Question enthuware.ocajp.i.v7.2.856 :

Posted: Fri Mar 03, 2017 10:06 pm
by admin
Updated. Thank you for your feedback!
Paul.