014-006-009
Method Call with super
Easy
Problem Description
Method Call with super
In this problem: You will create a Person class and an Employee class, where Employee's greet() method calls super.greet() to reuse the parent class behavior.
Learning Objective: Understand how to call the parent class method using super.methodName() within an overridden method
Overview
When overriding a method in a subclass, sometimes you want to add behavior on top of the parent class implementation rather than completely replacing it. Using super.methodName(), you can call the original parent class method from within the overridden method.
Specifications
Person Class
- Has a
String namefield - Constructor
Person(String name)initializes the field greet()method outputs"Hello, I am <name>."
Employee Class (extends Person)
- Adds a
String rolefield - Constructor
Employee(String name, String role)callssuper(name)and initializesrole - Overrides
greet()to first callsuper.greet(), then output"My role is <role>."
Main Class
- Creates an
Employeeobject withnew Employee("Tanaka", "Engineer") - Calls
greet()
Output Format
Hello, I am Tanaka.
My role is Engineer.
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