시작하기무료로 시작하기

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.

이 연습은 강의의 일부입니다

Input/Output and Streams in Java

강의 보기

연습 안내

  • 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.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

// 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");
    }
}
코드 편집 및 실행