Learn Before
Concept

Class Composition

Where inheritance represents an "Is-A" relationship, composition represents a "Has-A" relationship. For example, a Car is a type of Vehicle. To represent these as classes, Car would be a child class of Vehicle, inheriting some of its members (four tires, a steering wheel, etc.) and some of its methods (such as a method for Move() or Stop()). However, this isn't the only way to structure a relationship. You could represent a Car as a class that Has instances of other classes which provide it the functionality a Car would need. You could have two classes, called Move and Stop, which Car has an instance of in its constructor. Typically the class that Has the other classes is referred to as the Composite class, because it is composed of other classes (the Car is composed of Move and Stop). The classes that the Composite class contains are referred to as Components (the Move and Stop class are Components which make up the Car class).

0

1

Updated 2021-06-01

Tags

Python Programming Language

Data Science