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

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

Moderator: admin

Post Reply
RAZER-KIEV
Posts: 17
Joined: Thu Oct 01, 2015 4:06 pm
Contact:

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

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

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

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

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

ramon.carrascom
Posts: 19
Joined: Sun Aug 27, 2017 12:35 pm
Contact:

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

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

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

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

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

ramon.carrascom
Posts: 19
Joined: Sun Aug 27, 2017 12:35 pm
Contact:

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

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

touxito
Posts: 1
Joined: Fri Nov 03, 2017 5:04 am
Contact:

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

Post by touxito »

Hello, I don' t understand why protected int c(); is not accesible from another package if the class extends.

Greetens

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

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

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

natasci
Posts: 6
Joined: Fri Mar 15, 2019 4:40 am
Contact:

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

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

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

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

Post by admin »

The concept of polymorphism is applicable to instance methods only. So, yes, protected works differently for static members.
If you like our products and services, please help us by posting your review here.

docian
Posts: 2
Joined: Fri Jul 26, 2019 7:04 am
Contact:

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

Post 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!?

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

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

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

docian
Posts: 2
Joined: Fri Jul 26, 2019 7:04 am
Contact:

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

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

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

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

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

noeloo
Posts: 61
Joined: Sat Feb 15, 2020 8:56 am
Contact:

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

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

Dario_Castagnari
Posts: 2
Joined: Wed Mar 01, 2023 1:15 pm
Contact:

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

Post 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

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

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

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