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: 9174
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: 9174
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: 9174
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: 9174
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: 9174
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: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Rules 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: 53
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).

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests