About Question enthuware.ocajp.i.v8.2.856 :
Moderator: admin
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
About Question enthuware.ocajp.i.v8.2.856 :
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.
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.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.856 :
I don't see any contradiction. Both are two different things and are correct.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.856 :
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?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.856 :
So nothing. I just wanted to make sure that I finally have it clear in my mind. Thanks
-
- Posts: 21
- Joined: Mon Mar 02, 2020 3:38 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.856 :
Hi, regarding the explanation given.
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?
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.
What exactly is the reasoning behind this specific case.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 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?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.856 :
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.
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.
-
- Posts: 21
- Joined: Mon Mar 02, 2020 3:38 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.856 :
Very welladmin wrote: ↑Thu Jul 09, 2020 9:37 amYes, 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.

-
- Posts: 167
- Joined: Sun Apr 21, 2024 10:43 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.856 :
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:
for this question i just feel is playing with words
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);
}
}
Who is online
Users browsing this forum: No registered users and 3 guests