Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1203 :

Posted: Wed Jan 20, 2021 11:59 am
by Elnur_Aliyev
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.

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

Posted: Wed Jan 20, 2021 12:04 pm
by admin
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.

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

Posted: Mon Jan 02, 2023 7:11 am
by Chackingnoob
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.

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

Posted: Mon Jan 02, 2023 7:38 am
by admin
The problem statement is:
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.
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.

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.