Substituting the milk implementation without impacting the MilkTea class. In the following example, the MilkTea class contains an instance of Milk as an attribute.īy using composition, the MilkTea class can use the functionalities of the Milk class without inheriting its attributes and methods. On the other hand, composition is used when one class forms a “has-a” relationship with another class. fat_content = fat_content def prepare ( self ): print ( f 'Preparing milk with cream.' ) Class Milk : def _init_ ( self, name : str, fat_content : float ): self.