014-001-011

Basic Inheritance Syntax

Medium

Problem Description

Basic Inheritance Syntax

In this problem, you will create a program that defines a Dog class extending the Animal class using the extends keyword, reads the dog's name and sound from standard input, calls both parent and child class methods, and displays the result to standard output.

Learning Objective: Understand basic class inheritance using the extends keyword

Overview

Java supports class inheritance using the extends keyword. A child class inherits fields and methods from the parent class and can add its own members.

Specifications

  • Define an Animal class (name field, makeSound(String sound) method)
  • Define a Dog class extending Animal (add fetch() method)
  • Read the dog's name (line 1) and sound (line 2) from standard input
  • Create a Dog object and call both parent and child class methods

Input Format

<name>
<sound>

Output Format

Name: <name>
<name> says: <sound>
<name> fetches the ball!

Sample Input

Buddy
Woof!

Sample Output

Name: Buddy
Buddy says: Woof!
Buddy fetches the ball!

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