Tutorials  /  Python

Composition or Inheritance: Flexibility and Code Reusability Compared

Ccentron Redaktion · October 2024 ·5 min read ·Python, Tutorial

The concepts of composition and inheritance are fundamental building blocks of object-oriented programming. Both aim for code reusability but differ fundamentally in their implementation and application. The following sections explain and compare these two approaches to help you decide which one is appropriate for your needs.

Composition: "Has-a" Relationship

Composition allows you to create a "has-a" relationship between objects. This is achieved by using an object as an instance variable in another object. For example, a person can have a job object, as shown in the following Java example:

Code
public class Job {
    // Attributes and methods for the job
}

public class Person {
    // Composition: "Has-a" relationship
    private Job job;

    // Constructors, methods, etc.
}

In this example, the class Person has an instance of the class Job, which illustrates the composition between the two objects. This approach allows you to change or extend objects independently without directly affecting the structure of the other class.

VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Inheritance: "Is-a" Relationship

In contrast, inheritance establishes an "is-a" relationship. It allows a class to inherit the properties and methods of a parent class. This is accomplished using the extends keyword in Java. A classic example would be the inheritance of an Animal class by a Cat class:

Code
public class Animal {
    // Attributes and methods of the animal
}

public class Cat extends Animal {
    // Attributes and methods of the cat
}

Here, the class Cat inherits all attributes and methods from the class Animal. Inheritance is suitable when the subclass represents a specific extension of the superclass and requires all of the superclass's properties.

Composition versus Inheritance

While both approaches promote code reusability, composition offers greater flexibility and modularity since the relationships between objects are loosely coupled. Inheritance, on the other hand, creates tighter coupling, which can lead to problems if the superclass changes.

Example: Inheritance Can Lead to Problems

Let's look at the following example of inheritance:

Code
public class ClassA {
    public void foo() {}
}

public class ClassB extends ClassA {
    public void bar() {}
}

If the superclass ClassA is then extended to add a new method bar():

Code
public class ClassA {
    public void foo() {}

    public int bar() {
        return 0;
    }
}

this results in a compilation error since the method bar() in class ClassB has a different return type. This error could be avoided by using composition instead:

Code
public class ClassB {
    private ClassA classA = new ClassA();

    public void bar() {
        classA.foo();
        classA.bar();
    }
}

In this case, the implementation of class ClassB remains independent of changes in class ClassA, enhancing the maintainability and flexibility of the code.

Advantages of Composition

Another advantage of composition is the control over method access. In inheritance, all methods of the superclass are also accessible to the subclass and its users, which can pose potential security risks. However, with composition, you can selectively decide which methods are accessible to external classes:

Code
public class ClassB {
    private ClassA classA = new ClassA();

    public void foo() {
        classA.foo();
    }
}

Here, ClassB only exposes the foo() method of class ClassA and not all other methods. This provides more security and flexibility.

Complex Scenarios: Composition Offers More Flexibility

In more complex scenarios with multiple subclasses, composition also allows a more flexible method for method invocation without having to create an instance for each subclass. An example:

Code
abstract class Abs {
    abstract void foo();
}

public class ClassA extends Abs {
    public void foo() {}
}

public class ClassB extends Abs {
    public void foo() {}
}

public class Test {
    private Abs obj;

    public Test(Abs o) {
        this.obj = o;
    }

    public void foo() {
        this.obj.foo();
    }
}

By using an abstract type and composition, any subclass can be passed to the test class, making the code more modular and adaptable.

Composition Makes Testing Easier

A final important point is testing. Composition makes unit testing simpler because you know exactly which methods are used from other classes. These methods can be specifically isolated and tested. In contrast, with inheritance, you may have to test all methods of the superclass, leading to greater testing effort.

Conclusion: When Should You Choose Composition Over Inheritance?

The choice between composition and inheritance depends on the specific situation. Inheritance is useful when you are sure that the superclass will not change and that all subclasses need the same properties. Composition, on the other hand, offers more flexibility and is less prone to errors, especially in larger or dynamic projects. Use composition when you want loose coupling and more control over the code.

You now have an overview of the differences between composition and inheritance, as well as some reasons why composition is often preferred.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren