Abstract classes do not implicitly add abstract modifier
Even if abstract modifier was added, the Engine class compiles fine and the code prints ON OFF
If we do not make changes to turnON() and turnOFF() methods, then Operator class will not compile.
class abstract class Operator {
protected void turnON();
protected void turnOFF();
}
class EngineOperator extends Operator {
public final void turnON() {
System.out.println("ON");
}
public final void turnOFF() {
System.out.println("OFF");
}
}
class Engine {
Operator m = new EngineOperator();
public void operate() {
m.turnON();
m.turnOFF();
}
}
public class Test {
public static void main(String[] args) {
Engine carEngine = new Engine();
carEngine.operate();
}
}
Answer is C:
There are 2 ways to print output as ON/OFF:
1) methods turnON() and turnOFF() need to be declared as abstract.
or
2) methods turnON() and turnOFF() need to be declared as protected void turnON() {some code here...}.
Only then, EngineOperator class will be able to override these methods.
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
jduarte
Highly Voted 3 years, 9 months agoMPignaProTech
Most Recent 2 weeks, 3 days agoshivkumarx
9 months agoasdfjhfgjuaDCV
9 months, 1 week agoasdfjhfgjuaDCV
9 months agosteefaand
10 months, 1 week agor1muka5
1 year, 9 months agodexdinh91
2 years, 2 months agokarta
2 years, 4 months agoTarik2190
3 years, 7 months agoTarik2190
3 years, 7 months agoJME_CHG
3 years, 8 months agovarconite
4 years ago