DEV Community

Cover image for Inheritance vs Composition vs Aggregation
Adhiraj Kinlekar
Adhiraj Kinlekar

Posted on

Inheritance vs Composition vs Aggregation

The process of forming relations among entities is a crucial aspect of system design, playing a pivotal role in shaping the overall structure and functionality of a system. In the context of system design, entities represent distinct objects or concepts within the domain of the system.

To facilitate a deeper understanding of these relationships, let's explore the concept of association. Association is a technique for creating relationships between classes, where one class contains a reference to another class or object. This concept is fundamental to how entities interact, communicate, or collaborate within the system.

Aggregation

Aggregation is a weak form of association, where the contained class can exist independently of the containing class. The containing class is said to have a "has-a" relationship with the contained class.

In the given example, the Engine object is not "owned" by the Car class; rather, it's passed to the Car class through its constructor. The Car class simply holds a reference to the Engine object. The Car class doesn't control the Engine object's lifecycle. If the Car is destroyed, the Engine can still exist independently.

    public class Car 
    {
        private readonly Engine engine;

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

        public void Move() { engine.Start(); }

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

    public class Engine
    {
        public void Start() { /* code to start the engine */ }

        public void Stop() { /* code to stop the engine */ }
    };
Enter fullscreen mode Exit fullscreen mode

Composition

Composition, on the other hand, is a stronger form of association, where one class contains a reference to another class, and the contained class cannot exist independently of the containing class.

The containing class is said to have a "part-of" relationship with the contained class. For example, the Address class is associated with a particular user and cannot exist independently.

In composition, the class object's life cycle is fully controlled by the containing class, meaning when the containing class is destroyed, so is the contained class.

    public class User
    {
        private readonly string name;

        private readonly Address address;

        public User(string _name, int houseNo, string city, string state) { 

            name = _name;

            address = new Address(houseNo, city, state);
        }

        public string GetBasicUserInfo()
        {
            return name + " " + address.state;
        }
    };

    public class Address
    {
        public readonly int houseNo;

        public readonly string city;

        public readonly string state;

        public Address(int _houseNo, string _city, string _state)
        {
            houseNo = _houseNo;

            city = _city;

            state = _state;
        }
    };
Enter fullscreen mode Exit fullscreen mode

Inheritance

Another related concept in forming relationships is inheritance. Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (called a subclass or derived class) to acquire properties and behaviors (fields and methods) from another class (called a superclass or base class).

Inheritance establishes an "is-a" relationship between the base class and the derived class. For example, if you have a base class User and a derived class Employee, you can say that "an employee is a user".

    public class Employee : User
    {
        public readonly int employeeId;

        public Employee(int _employeeId, string _name, int houseNo, string city, string state) : base(_name, houseNo, city, state)
        {

            employeeId = _employeeId;
        }
    }
Enter fullscreen mode Exit fullscreen mode

In conclusion, the establishment of relationships among entities is at the core of effective system design. By defining connections and associations, we enable entities to interact, communicate, and collaborate within the system. It's crucial to recognize that the way entities are related has a profound impact on the system's efficiency, data integrity, and overall performance.

Song of the day: The Dear Hunter - The Most Cursed of Hands / Who Am I

Top comments (0)