About Question enthuware.ocajp.i.v8.2.1203 :
Moderator: admin
-
- Posts: 1
- Joined: Wed Jan 20, 2021 11:48 am
- Contact:
About Question enthuware.ocajp.i.v8.2.1203 :
I think "in the same package" should be added in this question, as it is not valid if superclass and subclass are in different packages.
-
- Site Admin
- Posts: 10385
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1203 :
It would not be invalid even if the classes are in different package because a subclass can still have a method with the same signature. But it would not be overriding because the base class's method would not be visible in the subclass.
-
- Posts: 1
- Joined: Mon Jan 02, 2023 7:07 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1203 :
But then the question "A method with no access modifier defined in a class can be overridden by a method marked protected (assuming that it is not final) in the sub class."
is defined wrong.
Like you said, it will no longer be overridden.
Either the question should state that the super- and subclass are in the same package and then the answer is true
or the question doesn't specify and then the answer is false because it won't be true for every case.
is defined wrong.
Like you said, it will no longer be overridden.
Either the question should state that the super- and subclass are in the same package and then the answer is true
or the question doesn't specify and then the answer is false because it won't be true for every case.
-
- Site Admin
- Posts: 10385
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1203 :
The problem statement is:
Of course, it does not mean that a method m1() can be overridden by a method m2() even though that is not specified in the question either, right? It is clear from the context of the question that we are talking about the same method m1 and m1 (not m1 and m2)! Similarly, the issue of packages is irrelevant here. It has no bearing on the rule about access modifiers.
The question is clearly about the role of access modifiers while overriding a method. And it is correct to say that a method with no access modifier can be overridden by a method marked protected because protected is more accessible than no access modifier. Hence the statement is true. So I don't understand what is the issue.A method with no access modifier defined in a class can be overridden by a method marked protected (assuming that it is not final) in the sub class.
Of course, it does not mean that a method m1() can be overridden by a method m2() even though that is not specified in the question either, right? It is clear from the context of the question that we are talking about the same method m1 and m1 (not m1 and m2)! Similarly, the issue of packages is irrelevant here. It has no bearing on the rule about access modifiers.
Who is online
Users browsing this forum: No registered users and 12 guests