016-002-010

Abstract and Concrete Methods

Easy

Problem Description

Abstract and Concrete Methods

In this problem: You will define an abstract class Appliance with an abstract method turnOn() and a concrete method displayStatus(), implement the abstract method in WashingMachine and Microwave, and call both methods.

Learning Objective: Understand the difference between abstract and concrete methods in abstract classes

Overview

The Appliance abstract class defines an abstract method turnOn() that requires different implementations per subclass, and a concrete method displayStatus() shared by all subclasses. WashingMachine and Microwave each implement turnOn() uniquely while inheriting the common displayStatus() behavior.

Specifications

Appliance Class (abstract)

  • Has a protected String name field
  • Constructor Appliance(String name) initializes the field
  • Abstract method abstract void turnOn(): implemented by subclasses
  • Concrete method displayStatus(): outputs "[name] is ready."

WashingMachine Class

  • Extends Appliance
  • turnOn(): outputs "WashingMachine: Starting wash cycle."

Microwave Class

  • Extends Appliance
  • turnOn(): outputs "Microwave: Heating started."

Main Class

  • Create WashingMachine("Washer-100") and Microwave("Micro-200")
  • Call displayStatus() and turnOn() in order on each object

Output Format

Washer-100 is ready.
WashingMachine: Starting wash cycle.
Micro-200 is ready.
Microwave: Heating started.

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up