014-004-005
Protected Modifier: Inheritance Access
Medium
Problem Description
Protected Modifier: Inheritance Access
In this problem, you will create a program that accesses a protected name field of the Animal class from the subclass Dog and displays the dog's name read from standard input.
Learning Objective: Understand the role of protected modifier
Overview
Protected members are accessible from the same class, same package, and subclasses. Learn how to use it in inheritance.
Specifications
- Define a
protected String namefield in Animal class - Access the parent's name from Dog class and display it in
showName()method - Read one line from standard input as the dog's name and display it
Input Format
dog name (one line)
Output Format
Dog name: [name]
Example
Input:
Buddy
Output:
Dog name: Buddy
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