014-001-006
Basic Class Inheritance
Easy
Problem Description
Basic class inheritance
In this problem, you will create a program that defines an Animal parent class with a name field and a makeSound() method, extends it with a Dog child class using the extends keyword, and reads a dog's name via Scanner to call the inherited method.
Learning Objective: Understand the basics of class inheritance using the extends keyword and constructor chaining with super
Overview
Create a child class (Dog) that inherits from a parent class (Animal), read a dog's name via Scanner, and call the inherited method.
Specifications
- Define a
namefield and constructorAnimal(String name)in theAnimalclass - Define
makeSound()method (outputs"{name} says: Some sound") Dogclass extendsAnimalclass with constructorDog(String name)(callssuper(name))- In
Mainclass, use Scanner to read a dog's name (1 line), create aDoginstance, and callmakeSound()
Input Format
Dog's name (1 line)
Output Format
{name} says: Some sound
Input/Output Example
Input: Rex
Output: Rex says: Some sound
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