014-004-008

Basics of protected Modifier

Easy

Problem Description

Basics of protected Modifier

In this problem, you will create a program that uses the protected modifier to define a field in the Animal class, accesses it from a Dog subclass, and displays a greeting message to standard output.

Learning Objective: Understand how to define fields accessible from subclasses using the protected modifier

Overview

The protected modifier is an access modifier that allows access from classes in the same package and from subclasses. It provides broader access than private but narrower than public.

Specifications

  • Define a protected field name in Animal class
  • Access and display the name field in Dog class (extends Animal)
  • Output a greeting using name in Dog's greet method
  • Read the dog's name from standard input and create a Dog object

Input Format

<name>
  • Line 1: dog's name (string)

Output Format

Animal name: <name>
Hello, I am <name> the dog!

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