Bird
0
0

You have a class Employee with fields name and salary. You want to create a class Manager that adds a department field and reuses Employee code. How should you use inheritance here?

hard📝 Application Q8 of 15
Java - Inheritance
You have a class Employee with fields name and salary. You want to create a class Manager that adds a department field and reuses Employee code. How should you use inheritance here?
ACreate Manager without inheritance and copy Employee fields
BMake Employee extend Manager to inherit department
CMake Manager extend Employee to inherit name and salary
DUse interfaces instead of inheritance for fields
Step-by-Step Solution
Solution:
  1. Step 1: Identify the base and derived classes

    Employee is the base class with common fields; Manager is a specialized Employee with extra field.
  2. Step 2: Apply inheritance correctly

    Manager should extend Employee to reuse name and salary fields and add department.
  3. Final Answer:

    Make Manager extend Employee to inherit name and salary -> Option C
  4. Quick Check:

    Subclass extends superclass to reuse fields [OK]
Quick Trick: Subclass extends superclass to reuse fields [OK]
Common Mistakes:
  • Reversing inheritance direction
  • Copying fields instead of inheriting
  • Using interfaces for data fields

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes