014-002-007
Super Keyword Basics
Easy
Problem Description
Super Keyword Basics
In this problem, you will create a program that defines an Animal class and a Dog subclass using the super keyword to invoke the superclass constructor and method, then displays the results to standard output. The dog's name and breed are read from standard input.
Learning Objective: Understand how to use the super keyword to call superclass constructors and methods
Overview
Create a Dog class that extends Animal, using the super keyword to invoke the superclass constructor and methods. Read the name and breed from standard input.
Specifications
Animal Class
- Field:
String name - Constructor: accepts
nameand initializes the field - Method:
speak()prints"Animal speaks"
Dog Class (extends Animal)
- Field:
String breed - Constructor: accepts
nameandbreed, callssuper(name)to initialize the superclass - Method: overrides
speak(), callssuper.speak()then prints"Dog barks" - Method:
info()prints"Name: {name}, Breed: {breed}"
Main Class
- Read the name from the first line of standard input and the breed from the second line
- Create a
Doginstance and callspeak()andinfo()
Input Format
{name}
{breed}
Output Format
Animal speaks
Dog barks
Name: {name}, Breed: {breed}
Example
Input:
Rex
Labrador
Output:
Animal speaks
Dog barks
Name: Rex, Breed: Labrador
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