Article image Classes and Objects: Composition of objects

17.9. Classes and Objects: Composition of objects

Page 53 | Listen in audio

17.9. Classes and Objects: Composition of Objects

Object-oriented programming is an approach to programming that involves creating objects that contain both data and methods. One of the main features of object-oriented programming is object composition, also known as class composition. Composition is a way of combining objects to create more complex data structures.

What is Object Composition?

Object composition is an object-oriented design principle that describes an "has a" relationship between different classes. Composition allows one class to contain an instance of another class. For example, if we have a class called "Car" and another class called "Engine", we can say that "Car" has an "Engine". This is the composition of objects.

How Does Object Composition Work?

In composition, one class (the composite class) contains an object of another class (the component class) as a field. The composite class has full control over the creation and lifetime of the component class object. The component class object cannot exist without the composite class object.

For example, consider a "Car" class that contains an "Engine" object. The "Engine" is initialized when the "Car" is created and destroyed when the "Car" is destroyed. The "Engine" cannot exist without the "Car".

Advantages of Composing Objects

Composing objects offers several advantages. First, it allows you to create complex data structures from simple classes. For example, you can create a "Car" class from "Engine", "Wheels" and "Seats" classes.

Second, composition promotes code reuse. If you have an "Engine" class that has already been tried and tested, you can reuse it in your "Car" class without having to rewrite or test the code again.

Third, composition allows you to change the behavior of a class without changing the class itself. For example, if you want to change the way "Car" works, you can simply replace "Engine" with a different one.

Object Composition Example

See an example of object composition in Java:

public class Engine {
    public void start() {
        System.out.println("The engine is running.");
    }
}

public class Car {
    personal Motor motor;

    public Car() {
        motor = new Motor();
    }

    public void drive() {
        engine.start();
        System.out.println("The car is driving.");
    }
}

In this example, the "Car" class contains an "Engine" object. The "Engine" is initialized when the "Car" is created. When the "drive" method is called on "Car", it calls the "start" method on "Engine".

This is a simple example, but object composition can be used to create much more complex data structures. The key is to understand that composition allows you to create classes that contain other classes, which can lead to a very rich and complex data structure.

In summary, object composition is a fundamental principle of object-oriented programming that allows you to create complex data structures from simple classes. Learning to use object composition effectively can make you a much more efficient and effective programmer.

Now answer the exercise about the content:

What is object composition in object-oriented programming?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Classes and Objects: Static Methods

Next page of the Free Ebook:

54Classes and Objects: Static Methods

3 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text