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

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

Moderator: admin

Post Reply
fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post by fasty23 »

Explanation said:
"protected
It will allow the subclasses and the classes in same package to access the
method."
I think this definition is for "default" not protected.
Is it right?

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

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

Post by admin »

No, it is correct. subclasses can be in any package while classes in the same package do not need to be subclasses.

thaednevol
Posts: 3
Joined: Thu Sep 27, 2018 7:48 am
Contact:

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

Post by thaednevol »

Hello!

I have this example:

package p1;

public class Base {
protected void show() {
System.out.println("Base show()");
}
}

And

package p2;

import p1.Base;

public class Child extends Base {
public void test() {
show(); //1

Base b = new Base();
b.show(); //2
}
}

The question states 'enable all the subclasses to access a method defined in the base class', but Child is a subclass of Base, but it cannot access the method defined in Base class, and it is defined as protected

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

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

Post by admin »

There is an important nuance associated with protected access. It does enable the child class to inherit (and thus accessible) the method defined in the superclass but that doesn't mean it can call that method on a base class reference. The subclass can call this method only on the child class reference. Technically, it can be called only on the type of the object in whose implementation this child class participates in. You can read more about it here: https://docs.oracle.com/javase/specs/jl ... #jls-6.6.2


Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests