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

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.

Moderators: Site Manager, fjwalraven

Post Reply
aureobm
Posts: 1
Joined: Thu Nov 13, 2014 11:37 am
Contact:

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

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Deleted User 2655

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

Post by Deleted User 2655 »

I completely agree!

AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

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

Post by AndaRO »

You must reformulate otherwise.

It's ambiguous.
Last edited by AndaRO on Fri Mar 03, 2017 3:13 pm, edited 1 time in total.

AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

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

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Updated. Thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 25 guests