Page 1 of 1

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

Posted: Fri Nov 06, 2015 5:46 am
by RAZER-KIEV
Good day!
I have question about constructors in this exemple.
As we see, there is no constructors in AccessTest class, it means that it will be provided by compiler, which inserts it whith "default" access modificator, so it willn't bee accessible from another package, isn't?

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

Posted: Fri Nov 06, 2015 8:17 pm
by admin
RAZER-KIEV wrote:Good day!
I have question about constructors in this exemple.
As we see, there is no constructors in AccessTest class, it means that it will be provided by compiler, which inserts it whith "default" access modificator
No, that is not correct. The access modifier of the constructor provided by the compiler is same as the access modifier of the class. Since the class is declared public, the default constructor will also be public.

HTH,
Paul.

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

Posted: Sun Aug 27, 2017 12:40 pm
by ramon.carrascom
Hi!
After reading answer's explanation, I still don't understand very well why C is also accessible by "ref". If I can't use "ref.c()", why are you including C as accessible? Is it because ref is a ClassTest object, and by this, it "implicitly" has c in it, although I can't reference it from ClassTester?

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

Posted: Sun Aug 27, 2017 10:38 pm
by admin
c() is not accessible. That is why option 3, which says "Only d() can be accessed by ref", is marked as the correct option.

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

Posted: Mon Aug 28, 2017 9:50 am
by ramon.carrascom
Oh, I'm so sorry, you're right!! I have confused the answers and thought the correct one was option 1 :roll: and I spent half an hour writing examples in Eclipse trying to find why C was also accessible :cry:

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

Posted: Fri Nov 03, 2017 5:06 am
by touxito
Hello, I don' t understand why protected int c(); is not accesible from another package if the class extends.

Greetens

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

Posted: Fri Nov 03, 2017 11:21 pm
by admin
A subclass can access the member even from another package. The restriction is only when the subclass (in another package) tries to access the member using a reference that is declared to be of type super class. In other words, a subclass cannot access a super class reference's protected member.
Check out the section of JLS mentioned in the explanation for more details.

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

Posted: Fri Mar 15, 2019 5:00 am
by natasci
Hi, I have also stumbled about this question, tried the code piece and accepted the explanation as logical. But after that, I' ve just declared the method c() in a parent class as "protected static" and seen, that now the method can be referenced in a subclass also through the ref of type super class. That confuses me and I can not understand such behavior. Have I missed something important? Thanks.

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

Posted: Fri Mar 15, 2019 5:18 am
by admin
The concept of polymorphism is applicable to instance methods only. So, yes, protected works differently for static members.

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

Posted: Fri Jul 26, 2019 7:21 am
by docian
admin wrote:
Fri Nov 03, 2017 11:21 pm
A subclass can access the member even from another package. The restriction is only when the subclass (in another package) tries to access the member using a reference that is declared to be of type super class. In other words, a subclass cannot access a super class reference's protected member.
Check out the section of JLS mentioned in the explanation for more details.
seems that this rule is not in place for static members. is it right!?

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

Posted: Fri Jul 26, 2019 7:33 am
by admin
docian wrote:
Fri Jul 26, 2019 7:21 am
admin wrote:
Fri Nov 03, 2017 11:21 pm
A subclass can access the member even from another package. The restriction is only when the subclass (in another package) tries to access the member using a reference that is declared to be of type super class. In other words, a subclass cannot access a super class reference's protected member.
Check out the section of JLS mentioned in the explanation for more details.
seems that this rule is not in place for static members. is it right!?
Please read the post just above yours.

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

Posted: Sat Jul 27, 2019 6:38 am
by docian
admin wrote:
Fri Jul 26, 2019 7:33 am
docian wrote:
Fri Jul 26, 2019 7:21 am
admin wrote:
Fri Nov 03, 2017 11:21 pm
A subclass can access the member even from another package. The restriction is only when the subclass (in another package) tries to access the member using a reference that is declared to be of type super class. In other words, a subclass cannot access a super class reference's protected member.
Check out the section of JLS mentioned in the explanation for more details.
seems that this rule is not in place for static members. is it right!?
Please read the post just above yours.
I've seen the answer. the question is: how it is working for static members and what's the rule?

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

Posted: Sat Jul 27, 2019 9:35 pm
by admin
Rule is that protected static members can be accessed from a subclass using superclass or subclass reference.

Calls to static members are bound at compile time, so, the rule about protected access to instance members does not apply to static members. No reference is required to access static members anyway. If you use a reference to call a static member, it is resolved at compile time to the static method as per the declared type of the reference.

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

Posted: Sat Jan 16, 2021 4:43 am
by noeloo
protected members of a class are accessible outside the package only in subclasses of that class, and only when they are fields of objects that are being implemented by the code that is accessing them
I somehow cannot get my head around this sentence (I understand all the rest).
They are accessible in subclasses of subclasses - why?
It seems that in this case (I suppose the code at the end of explanation is still inside the AccessTester class?) they are fields of object (type: SubAccessTester) which is implemented somewhere else (not by the code accessing them).

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

Posted: Sat Mar 04, 2023 4:37 am
by Dario_Castagnari
no way people: protected methods of a superclass are visible in their subclasses (classes extending them), regardless of the package where the subclasses are defined or the static or dynamic nature of the methods. Even though I knew it, I also tried in my local IDE and confirmed this obvious rule therefore, the answer 3 is wrong, the answer 1 is right.

Please fix this bug asap.

Thank you in advance

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

Posted: Sat Mar 04, 2023 8:40 am
by admin
This is not a bug. It is a bit complicated to grasp so please do read the explanation carefully (multiple times, if required). It is correct.

Option 3 is correct. Try compiling the given code exactly as given and verify yourself.