Page 1 of 1
About Question enthuware.ocajp.i.v8.2.856 :
Posted: Thu Dec 06, 2018 6:45 pm
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.
Re: About Question enthuware.ocajp.i.v8.2.856 :
Posted: Thu Dec 06, 2018 10:41 pm
by admin
I don't see any contradiction. Both are two different things and are correct.
Re: About Question enthuware.ocajp.i.v8.2.856 :
Posted: Fri Dec 07, 2018 1:43 pm
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?
Re: About Question enthuware.ocajp.i.v8.2.856 :
Posted: Fri Dec 07, 2018 3:11 pm
by admin
no. so?
Re: About Question enthuware.ocajp.i.v8.2.856 :
Posted: Fri Dec 07, 2018 3:33 pm
by OCAJO1
So nothing. I just wanted to make sure that I finally have it clear in my mind. Thanks
Re: About Question enthuware.ocajp.i.v8.2.856 :
Posted: Thu Jul 09, 2020 8:53 am
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?
Re: About Question enthuware.ocajp.i.v8.2.856 :
Posted: Thu Jul 09, 2020 9:37 am
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.
Re: About Question enthuware.ocajp.i.v8.2.856 :
Posted: Thu Jul 09, 2020 9:47 am
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

Re: About Question enthuware.ocajp.i.v8.2.856 :
Posted: Sun Aug 11, 2024 6:02 am
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