Bird
0
0

Which OOP principle helps you avoid repeating code for name and salary in each class?

hard📝 Application Q8 of 15
Java - Object-Oriented Programming Concepts
You want to design a Java class hierarchy for different types of employees: Manager, Developer, and Intern. All employees have a name and salary, but only Managers have a bonus. Which OOP principle helps you avoid repeating code for name and salary in each class?
AAbstraction
BEncapsulation
CPolymorphism
DInheritance
Step-by-Step Solution
Solution:
  1. Step 1: Identify common features

    Name and salary are common to all employee types, so they should be in a shared base class.
  2. Step 2: Apply OOP principle to reuse code

    Inheritance allows subclasses (Manager, Developer, Intern) to reuse code from a parent Employee class, avoiding repetition.
  3. Final Answer:

    Inheritance -> Option D
  4. Quick Check:

    Inheritance = reuse common code in subclasses [OK]
Quick Trick: Use inheritance to share common features [OK]
Common Mistakes:
  • Confusing encapsulation with code reuse
  • Thinking polymorphism avoids code repetition
  • Assuming abstraction handles code reuse

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes