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?
About Question enthuware.ocajp.i.v7.2.1330 :
Moderator: admin
-
fasty23
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
-
admin
- Site Admin
- Posts: 10446
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1330 :
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 :
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
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 :
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
-
admin
- Site Admin
- Posts: 10446
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1330 :
You can see some other questions also: https://enthuware.com/forum/viewtopic.php?f=2&t=3507
and https://enthuware.com/forum/viewtopic.php?f=2&t=3957
and https://enthuware.com/forum/viewtopic.php?f=2&t=3957
Who is online
Users browsing this forum: No registered users and 23 guests