Java Interview Questions

30 Questions
Java

Java

Web DevelopmentBackend

Question 15

What is the difference between composition and inheritance?

Answer:

Composition and inheritance are both fundamental concepts in object-oriented programming that are used to establish relationships between classes. However, they serve different purposes and have distinct characteristics. Here’s a detailed comparison:

Inheritance

Definition: Inheritance is a mechanism where a new class (subclass) inherits the properties and behaviors (fields and methods) of an existing class (superclass).

Key Points:

  • "Is-a" Relationship: Inheritance represents an "is-a" relationship. For example, a Dog is an Animal.
  • Code Reusability: Allows reuse of code from the superclass.
  • Overriding: Subclasses can override methods of the superclass to provide specific implementations.
  • Single Inheritance: In Java, a class can only inherit from one superclass (single inheritance).

Example:

public class Animal {
    public void makeSound() {
        System.out.println("Animal sound");
    }
}

public class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Bark");
    }
}

In this example, Dog inherits from Animal and overrides the makeSound method.

Composition

Definition: Composition is a design principle where a class is composed of one or more objects of other classes, meaning it has references to other objects as its members.

Key Points:

  • "Has-a" Relationship: Composition represents a "has-a" relationship. For example, a Car has an Engine.
  • Flexibility: Provides more flexibility than inheritance as it allows changes in the composed classes without affecting the composed class.
  • Reuse: Encourages code reuse by combining existing classes to create more complex functionalities.
  • Encapsulation: Promotes better encapsulation by keeping the components separate and limiting direct access.

Example:

public class Engine {
    public void start() {
        System.out.println("Engine started");
    }
}

public class Car {
    private Engine engine;

    public Car() {
        engine = new Engine();
    }

    public void start() {
        engine.start();
        System.out.println("Car started");
    }
}

In this example, Car is composed of an Engine object, establishing a "has-a" relationship.

Key Differences

  1. Relationship Type:

    • Inheritance: "Is-a" relationship (e.g., Dog is an Animal).
    • Composition: "Has-a" relationship (e.g., Car has an Engine).
  2. Coupling:

    • Inheritance: Creates a tight coupling between superclass and subclass. Changes in the superclass can impact all subclasses.
    • Composition: Promotes loose coupling. Components can be changed or replaced independently.
  3. Flexibility:

    • Inheritance: Less flexible due to the rigid hierarchy. Difficult to change superclass without affecting subclasses.
    • Composition: More flexible. Components can be modified or replaced without impacting the composed class.
  4. Code Reusability:

    • Inheritance: Reuses the code from the superclass. However, can lead to inheritance hierarchies that are hard to manage.
    • Composition: Reuses code by containing instances of other classes, promoting greater modularity and reuse.
  5. Design Complexity:

    • Inheritance: Simpler to implement but can lead to complex hierarchies.
    • Composition: Can be more complex to design but results in more maintainable and adaptable code.

When to Use

Inheritance:

  • Use inheritance when there is a clear hierarchical relationship where the subclass is a type of the superclass.
  • When you want to leverage polymorphism and method overriding.
  • Example: Animal and Dog, where Dog is a type of Animal.

Composition:

  • Use composition when you want to build complex types by combining objects.
  • When you need more flexibility and better encapsulation.
  • When you want to reuse functionalities from different classes without creating a rigid hierarchy.
  • Example: Car and Engine, where Car has an Engine.

Conclusion

Both composition and inheritance are essential concepts in OOP, and each has its advantages and use cases. Inheritance is suitable for defining a clear "is-a" relationship and leveraging polymorphism, while composition is ideal for building flexible and maintainable code through "has-a" relationships and better encapsulation. Understanding when and how to use each approach is crucial for designing robust and efficient object-oriented systems.

Recent job openings