About Question enthuware.ocajp.i.v7.2.1007 :

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

Moderator: admin

Post Reply
jbilkes
Posts: 21
Joined: Tue Sep 09, 2014 3:28 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1007 :

Post by jbilkes »

uhm, this is plain wrong if you ask me...i answered option 1, 2, 6 and 6 is indeed wrong since of course static and abstract cant be together. But, it is asked that "all the members of the same package have access to it"...clearly, this isnt the case with protected since not every class in the same package has an inheritance relationship to this method's class

any explanation? i would say ask for 2 options instead of 3 et voila, problem solved, good question..

EDIT:

Ok i tested it and, believe it or not, this is the moment i finally get that protected really incorporates 'package private' ... i mean, till now every book or teacher whom talked to me about it said package private means that in the package every class has access, and protected on the other hand means that the childs have access...im pretty sure that at no point it is mentioned that protected ALSO means every class in the package...how difficult would it be to make things clear?? while it was said that private, package private, protected, public was the order how things worked..it was never EXPLICITELY mentioned that protected incorporated package private...even the teacher didnt know the order at some point because its never mentioned clear enough....how is this possible, why do i have to do all the work here

anyhow, thanx guys I understand a little bit more of Java's quirks, although i had to do some testing myself ;-)

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

Re: About Question enthuware.ocajp.i.v7.2.1007 :

Post by admin »

Hi,
There are several questions in our question bank that touch upon this aspect. Explanations for these questions such as Qid 2.966, explain how protected works.
Members with default accessibility are only accessible within the class itself and from classes in the same package.
Protected members are in addition accessible from subclasses. Members with private accessibility are only accessible within the class itself.
Explanation to question with Qid 2.978 says,
public > protected > package (i.e. no modifier) > private
where public is least restrictive and private is most restrictive.

Remember:
protected is less restrictive than package access. So a method(or field) declared as protected will be accessible from a subclass even if the subclass is not in the same package.
The same is not true for package access.
A top level class can only have either ///public/// or no access modifier but a method or field can have all the four. Note that ///static///, ///final///, ///native/// and ///synchronized/// are not considered as access modifiers.
Explanation to qid 2.1217 says,
'protected' means the method will be accessible to all the classes in the same package and all the subclasses (even if the subclass is in a different package).
I was alarmed by your claim that no book makes this clear so I looked for this in a very popular certification book and while explaining default and protected access on page 33 it clearly says,
The protected and default access control levels are almost identical, but with one
critical difference. A default member may be accessed only if the class accessing the
member belongs to the same package, whereas a protected member can be accessed
(through inheritance) by a subclass even if the subclass is in a different package.

...

Default and protected behavior differ only when we talk about subclasses. If the
protected keyword is used to define a member, any subclass of the class declaring
the member can access it through inheritance. It doesn't matter if the superclass and
subclass are in different packages, the protected superclass member is still visible to
the subclass (although visible only in a very specific way as we'll see a little later).
This is in contrast to the default behavior, which doesn't allow a subclass to access a
superclass member unless the subclass is in the same package as the superclass.
...
But when you think protected, think package + kids. A class with a
protected member is marking that member as having package-level access for all
classes, but with a special exception for subclasses outside the package.
I have not investigated your claim thoroughly using other books but I am confident that any book rated 4+ stars would have included this information with good clarity.

HTH,
Paul.

jbilkes
Posts: 21
Joined: Tue Sep 09, 2014 3:28 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1007 :

Post by jbilkes »

thx for this extensive answer

I guess I was just given crappy study material (and teachers...no joke he made the mistake himself thinking package private was less restrictive than protected)

;-)

barper
Posts: 1
Joined: Mon Jun 22, 2015 5:18 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1007 :

Post by barper »

Hehehe, tricky question.

Appreciate the in depth answer here, has helped me to understand it better :)

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests