014-001-009
Extending Classes with Inheritance
Easy
Problem Description
Extending Classes with inheritance
Learning Objective: Understand the basic syntax of inheritance using the extends keyword
In this problem, you will create a program that defines a Dog class inheriting from the Animal class using the extends keyword, reads a name from Scanner, calls the parent class's speak() method, and displays the result to standard output.
Overview
Inheritance is a mechanism that allows you to create a new class (child class) by inheriting the features of an existing class (parent class). The extends keyword is used to define the inheritance relationship.
Specifications
- Read the animal's name from standard input (one line)
- Define a
namefield andsetName(String name)method in the Animal class - Define the speak() method in Animal class to print "[name] speaks"
- Create the Dog class by inheriting from the Animal class
- Create a dog instance in the Main class, set the name, and execute speak()
Input Format
[animal name]
Output Format
[name] speaks
Example
Input:
Buddy
Output:
Buddy speaks
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