You are currently viewing Composition vs Inheritance in Java: Explained with Code Examples

Composition vs Inheritance in Java: Explained with Code Examples

  • Post author:
  • Post category:Java
  • Post comments:0 Comments
  • Post last modified:May 14, 2024

Introduction

When designing object-oriented software in Java, developers often encounter the dilemma of choosing between composition and inheritance for code reuse and building relationships between classes. Both techniques have their advantages and drawbacks, and understanding when to use each is crucial for writing maintainable and flexible code.

In this tutorial, we’ll explore the concepts of composition and inheritance in Java, compare them, and provide code examples to illustrate their usage in different scenarios.

What is Composition?

Composition is a design technique where a class contains an object of another class as one of its fields. This allows the containing class to delegate certain responsibilities to the contained object, promoting code reuse and flexibility.

What is Inheritance?

Inheritance, on the other hand, is a mechanism in Java where a class (subclass) can inherit properties and behavior (methods) from another class (superclass). The subclass can extend the functionality of the superclass by adding new methods or overriding existing ones.

When to Use Composition?

Composition is typically preferred in situations where:

  1. Loose coupling is desired between classes.
  2. There is a has-a relationship between the containing class and the contained class.
  3. The contained class behavior may need to be dynamically changed at runtime.
  4. You want to avoid the limitations of multiple inheritance.

Let’s illustrate this with an example:

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

class Car {
    private Engine engine;

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

    public void start() {
        engine.start();
    }
}

In this example, the Car class has a has-a relationship with the Engine class. Instead of inheriting from Engine, Car contains an instance of Engine, allowing it to delegate the responsibility of starting the engine.

When to Use Inheritance?

Inheritance is suitable when:

  1. There is an is-a relationship between the subclass and superclass.
  2. You want to reuse code and behavior from the superclass.
  3. You need to extend or customize the behavior of the superclass.

Consider the following example:

class Animal {
    public void eat() {
        System.out.println("Animal is eating");
    }
}

class Dog extends Animal {
    public void bark() {
        System.out.println("Dog is barking");
    }
}

Here, Dog is-a kind of Animal, so inheritance is a natural choice. Dog inherits the eat() method from Animal and adds its own behavior with the bark() method.

Conclusion

In summary, composition and inheritance are two fundamental concepts in object-oriented programming, each with its own strengths and use cases. By understanding the differences between them and when to apply each approach, you can design more robust and maintainable Java applications.

Remember, composition promotes flexibility and loose coupling, while inheritance facilitates code reuse and specialization. Choose the approach that best fits the requirements of your project and design goals.

Leave a Reply