Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q7 of 15
Java - Abstraction
Find the error in this code snippet:
abstract class Machine {
    abstract void operate();
    void start() {
        System.out.println("Machine started");
    }
}
class Printer extends Machine {
    void operate() {
        System.out.println("Printing");
    }
    void start() {
        System.out.println("Printer started");
    }
}
public class Test {
    public static void main(String[] args) {
        Machine m = new Printer();
        m.start();
    }
}
AAbstract method operate() not implemented.
BNo error; code runs and prints 'Printer started'.
CCannot override start() method in subclass.
DMachine class cannot have concrete methods.
Step-by-Step Solution
Solution:
  1. Step 1: Check method overrides

    Printer overrides start() method; this is allowed.
  2. Step 2: Verify abstract method implementation

    Printer implements operate(), fulfilling abstract method requirement.
  3. Final Answer:

    No error; code runs and prints 'Printer started'. -> Option B
  4. Quick Check:

    Overriding concrete methods allowed = C [OK]
Quick Trick: Concrete methods can be overridden in subclasses [OK]
Common Mistakes:
  • Thinking concrete methods can't be overridden
  • Assuming abstract methods not implemented
  • Believing abstract class can't have concrete methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes