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 name field
  • Constructor Person(String name) initializes the field
  • greet() method outputs "Hello, I am <name>."

Employee Class (extends Person)

  • Adds a String role field
  • Constructor Employee(String name, String role) calls super(name) and initializes role
  • Overrides greet() to first call super.greet(), then output "My role is <role>."

Main Class

  • Creates an Employee object with new 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