014-001-005
Inheritance: Extending Animal Class
Medium
Problem Description
Inheritance: Extending Animal Class
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:
- Create Dog instance with name "Buddy"
- Output name
- Call
makeSound() - Call
wagTail()
Output Format
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?
