LeetCampus
Interview Question

Although inheritance is a popular OOPs concept, it is less advantageous than composition. Explain.

July 24, 2025
0 views
Difficulty: Medium
Popularity: Common
Share on

Question Explanation

Inheritance and composition are two fundamental concepts in Object-Oriented Programming (OOP). While inheritance allows a class to inherit properties and methods from another class, composition involves creating complex types by combining objects of other types. Interviewers often ask this question to assess a candidate's understanding of these concepts and their implications in software design. A common misconception is that inheritance is always the superior choice for code reuse. However, this perspective can lead to rigid and tightly coupled designs, which are harder to maintain and extend. Composition, on the other hand, promotes flexibility and reusability by allowing objects to be composed at runtime. This question also tests a candidate's ability to think critically about design patterns and principles, such as the SOLID principles, particularly the Liskov Substitution Principle and Dependency Inversion Principle. Understanding when to use inheritance versus composition is crucial for building scalable and maintainable software. It encourages developers to think about the relationships between classes and how to best structure their code for future enhancements. Ultimately, this question evaluates not just knowledge of OOP concepts but also practical application in real-world scenarios.

Sample Answers

Example 1: Flexibility and Maintainability

When considering inheritance versus composition, flexibility is a key advantage of composition. For instance, if you develop a Car class using inheritance, you might create subclasses for ElectricCar and GasCar. However, this can lead to a rigid hierarchy. If later you want to add a feature like autonomous driving, you would need to modify multiple classes. In contrast, using composition allows you to create an AutonomousDriving class and combine it with any car type. This approach enhances maintainability, as changes can be made independently without altering the entire class hierarchy. Additionally, composition promotes code reuse and reduces duplication. By composing objects, you can easily swap behaviors or features at runtime, making your application more adaptable to changes. This flexibility is particularly valuable in large-scale applications where requirements evolve frequently.

Example 2: Avoiding Fragile Base Class Problem

One significant disadvantage of inheritance is the fragile base class problem, which occurs when changes to a base class inadvertently affect derived classes. For example, if you have a base class Animal with a method makeSound, and you change its implementation, it could break functionality in all derived classes like Dog and Cat. This issue can lead to buggy behavior and increased maintenance costs. However, using composition can mitigate this risk. By composing an Animal with a SoundBehavior interface, you can easily change how an animal makes a sound without modifying the Animal class itself. This separation of concerns allows for safer code evolution and minimizes the chances of introducing bugs when making changes. Furthermore, it encourages adherence to the Open/Closed Principle, where classes should be open for extension but closed for modification.

Example 3: Real-World Applications and Performance

In practical software development, composition is often favored for its real-world applicability. For example, in a large-scale e-commerce application, you might have various product types like Book, Electronics, and Clothing. Instead of creating a complex inheritance hierarchy, you can use composition to create a Product class that includes Pricing, Inventory, and Review components. This modular approach allows teams to work on different components in parallel without stepping on each other's toes. Additionally, from a performance perspective, composition can be more efficient. When using inheritance, the entire class hierarchy is loaded into memory, which can be resource-intensive. In contrast, with composition, you only load the necessary components, optimizing performance. This strategy not only leads to better resource management but also fosters a more agile development environment where features can be added or modified with minimal impact on the overall system.

Keywords

inheritancecompositionOOPsoftware designSOLID principles

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions