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 colorfield - Constructor
Shape(String color)initializes the field
ColoredCircle Class
- Extends
Shape - Has a
private double radiusfield - Constructor
ColoredCircle(String color, double radius)callssuper(color) describe()method: directly accesses the protectedcolorfield and outputs in the format below
Main Class
- Creates a
ColoredCircleinstance and callsdescribe()
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