Bird
0
0

Given this code:

hard📝 Application Q9 of 15
Java - Abstraction
Given this code:
abstract class Device {
    abstract void turnOn();
    void status() {
        System.out.println("Device is ready");
    }
}
class Phone extends Device {
    void turnOn() {
        System.out.println("Phone is on");
    }
}
class Laptop extends Device {
    void turnOn() {
        System.out.println("Laptop is on");
    }
}
public class Test {
    public static void main(String[] args) {
        Device d1 = new Phone();
        Device d2 = new Laptop();
        d1.status();
        d2.turnOn();
    }
}

What is the output?
ADevice is ready Device is ready
BPhone is on Laptop is on
CCompilation error
DDevice is ready Laptop is on
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method calls on objects

    d1.status() calls concrete method in Device printing 'Device is ready'.
  2. Step 2: Analyze the second method call

    d2.turnOn() calls the overridden method in Laptop printing 'Laptop is on'.
  3. Final Answer:

    Device is ready Laptop is on -> Option D
  4. Quick Check:

    Concrete + overridden methods output combined [OK]
Quick Trick: Concrete methods run from abstract class; overridden methods from subclass [OK]
Common Mistakes:
  • Expecting abstract method output for status()
  • Confusing which method is called
  • Assuming compilation error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes