014-004-010

Accessing protected Fields

Easy

Problem Description

Accessing protected Fields

In this problem: You will define a protected field color in a Shape class and access it directly from a child class ColoredCircle to display shape information.

Learning Objective: Understand that protected fields can be accessed directly from child classes

Overview

Define a protected field color (String type) in the Shape class. The ColoredCircle class inherits from Shape and directly accesses the color field in its describe() method to output the shape description.

Specifications

Shape Class

  • Has a protected String color field
  • Constructor Shape(String color) initializes the field

ColoredCircle Class

  • Extends Shape
  • Has a private double radius field
  • Constructor ColoredCircle(String color, double radius) calls super(color)
  • describe() method: directly accesses the protected color field and outputs in the format below

Main Class

  • Creates a ColoredCircle instance and calls describe()

Output Format

Shape: Circle
Color: [color]
Radius: [radius]

Examples

Example 1

Shape: Circle
Color: Red
Radius: 5.0

Example 2

Shape: Circle
Color: Blue
Radius: 3.5

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