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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

Need a quick clarification please.

They following are explanations for choices 3 and 4. Are the underlined portions contradictory or am I missing something?

It can also be modified from any class defined in the same package.

Note that since value is protected, a class in another package which extends MyClass will only inherit this variable, but it cannot read or modify the value of a variable of a MyClass instance.

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

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

Post by admin »

I don't see any contradiction. Both are two different things and are correct.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

Then let me put it this way, when an instance or static field is protected, is there any situation that (besides being inherited) it can be read or modified by a class in a different package that is extending that field's class?

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

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

Post by admin »

no. so?

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

So nothing. I just wanted to make sure that I finally have it clear in my mind. Thanks

Sieusc
Posts: 21
Joined: Mon Mar 02, 2020 3:38 am
Contact:

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

Post by Sieusc »

Hi, regarding the explanation given.

Code: Select all

//in different package 
class X extends MyClass{     
public static void main(String[] args){        
int a = new MyClass().value; //This will not compile because X does not own MyClass's value.
a = new X().value; //This will compile fine because X inherits value.
Note that since value is protected, a class in another package which extends MyClass will only inherit this variable, but it cannot read or modify the value of a variable of a MyClass instance.
What exactly is the reasoning behind this specific case.

What is the impact of the "different pacakge" on this matter. Because let's say X is in the same package as MyClass, and the main method is executed in class X just like above case. Then X would still not own MyClass's value, just like above it would still only own the inherited value?

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

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

Post by admin »

Yes, it would still own only the inherited value but if the class is in the same package, it is allowed to access protected fields of other classes of the same package anyway.

The reasoning is simple. A class should not be able to mess with its super class's data if the super class doesn't want a subclass to do so. Classes belonging to the same package have "extra power" to do that. That's what the language designers thought was the best approach.

Sieusc
Posts: 21
Joined: Mon Mar 02, 2020 3:38 am
Contact:

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

Post by Sieusc »

admin wrote:
Thu Jul 09, 2020 9:37 am
Yes, it would still own only the inherited value but if the class is in the same package, it is allowed to access protected fields of other classes of the same package anyway.

The reasoning is simple. A class should not be able to mess with its super class's data if the super class doesn't want a subclass to do so. Classes belonging to the same package have "extra power" to do that. That's what the language designers thought was the best approach.
Very well :thumbup:

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

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

Post by raphaelzintec »

this is not clear: It can be read and modified from any class within the same package.

because a subclass can have read and change value check here:

Code: Select all

public class Bob extends MyClass {
    public static void main(String[] args) {
        int b = new Bob().value;
        System.out.println(b);
    }
}
for this question i just feel is playing with words

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests