Page 1 of 1

About Question enthuware.ocpjp.v17.2.3726 :

Posted: Fri Jun 09, 2023 4:07 pm
by edufin166@yahoo.com
In this question we have:

class A extends Base{
@Override
int power(){ // Class A method power returning "0"
return 0;
}
}

if(base instanceof A a && a.power() == 1 ){     //
((AA)a).mAA();
}      

In the IF above...
How can I have "a.power() == 1" in the If-Conditional above as TRUE, and therefore entering in the conditional and executing ((AA)a).mAA(),
if in the "Class A" the method power return 0???

Re: About Question enthuware.ocpjp.v17.2.3726 :

Posted: Fri Jun 09, 2023 10:48 pm
by admin
Right, so a.power() == 1 will be false. The control won't enter the if block and ((AA)a).mAA(); won't be executed.
But what is the issue? It is still valid code. It is also possible that a may point to an instance of AA, which return 1.