014-004-009

Overriding Protected Methods

Medium

Problem Description

Overriding Protected Methods

In this problem, you will create a program that overrides the protected method getArea() from the Shape class in the Circle and Rectangle subclasses to calculate each shape's area from values read via standard input and display the results.

Learning Objective: Understand how to override protected methods in subclasses and change access scope

Overview

Protected methods can be overridden in subclasses, and the access modifier can be changed to protected or public (cannot be more restrictive).

Specifications

  • Define protected method getArea() in Shape class (returns 0.0)
  • override getArea() in Circle class to calculate circle area
  • override getArea() in Rectangle class to calculate rectangle area
  • Display each shape's area using public displayArea()
  • Read radius, width, and height as double values from standard input

Input Format

radius (double)
width (double)
height (double)

Output Format

Circle area: <area>
Rectangle area: <area>

Area values are printed with 2 decimal places.

Sample Input/Output

Input:

5.0
4.0
6.0

Output:

Circle area: 78.54
Rectangle area: 24.00

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