IniziaInizia gratis

Implementing method overriding

You will implement method overriding to modify inherited behavior. You will create a parent class Device and a subclass Phone that overrides a method.

Questo esercizio fa parte del corso

Input/Output and Streams in Java

Visualizza il corso

Istruzioni dell'esercizio

  • Create Phone class inherited from parent class Device.
  • Override the .turnOn() method.
  • Create a new instance of Phone.
  • Calling the new method to print the message.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

// Define Phone class extending Device
class Phone ____ Device { 
    // Override the turnOn() method
    @____
    void turnOn() {
        System.out.println("Phone is turning on");
    }
    public static void main(String[] args) {
    	// Create instance of Phone
        Phone p = new ____(); 
        
        //Call turnOn() from phone instance
        p.____();
    }
}

class Device {
    void turnOn() {
        System.out.println("Device is turning on");
    }
}
Modifica ed esegui il codice