014-001-005
Inheritance: Extending Animal Class
Medium
Problem Description
inheritance: Extending Animal class
In this problem, you will create a program that implements an Animal parent class and a Dog child class using inheritance, reads the dog's name from standard input, creates a Dog instance, and calls each method to display the results to standard output.
Learning Objective: Understand how to extend class functionality using inheritance
Overview
Inheritance is a fundamental concept in object-oriented programming that allows creating new classes based on existing ones. Child classes inherit parent class functionality while adding their own features.
Specifications
Create parent class Animal:
- Private field
name - constructor accepts name
makeSound()method: outputs "Some sound"getName()method: returns name
Create child class Dog:
- Extends
Animal - Constructor accepts name and passes to parent constructor
- Unique method
wagTail(): outputs "Wagging tail"
In Main class:
- Read the dog's name from standard input (one line)
- Create Dog instance with the read name
- Output name
- Call
makeSound() - Call
wagTail()
Input Format
<dog name>
Output Format
Name: <name>
Some sound
Wagging tail
Sample I/O
Input:
Buddy
Output:
Name: Buddy
Some sound
Wagging tail
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