Object relationship: Difference between revisions
From Rice Wiki
(Created page with "In OOP, interrelated objects are composed together for complex functionality. This page describes concepts that relate one object to another. = Inheritance = = Composition = = Aggregation = Unlike") |
No edit summary |
||
Line 2: | Line 2: | ||
= Inheritance = | = Inheritance = | ||
Implementation inheritance extends the implementation of a parent class with a child class that inherit the members of the parent class while adding/overriding certain members. | |||
= Composition = | = Composition = | ||
Unlike [[Object relationship#Inheritance|inheritance]], which directly extends a parent object, composition owns the object whose functionality it wishes to extend. This solves many of the issues of inheritance.<syntaxhighlight lang="c++"> | |||
class Owner | |||
{ | |||
Extended ext; | |||
public: | |||
Owner() { | |||
ext = Extended(); | |||
} | |||
}; | |||
</syntaxhighlight> | |||
= Aggregation = | = Aggregation = | ||
Similar to [[Object relationship#Composition|composition]], aggregation involves an object "having" another object. The difference between the two lies in that composition focuses on a parent-child relationship, whereas Aggregation |
Revision as of 17:51, 8 August 2024
In OOP, interrelated objects are composed together for complex functionality. This page describes concepts that relate one object to another.
Inheritance
Implementation inheritance extends the implementation of a parent class with a child class that inherit the members of the parent class while adding/overriding certain members.
Composition
Unlike inheritance, which directly extends a parent object, composition owns the object whose functionality it wishes to extend. This solves many of the issues of inheritance.
class Owner
{
Extended ext;
public:
Owner() {
ext = Extended();
}
};
Aggregation
Similar to composition, aggregation involves an object "having" another object. The difference between the two lies in that composition focuses on a parent-child relationship, whereas Aggregation