What is Composition (Has-A Relation)?

It is one of the fundamental concepts of Object-Oriented Programming. In this concept, we will describe a class that references to one or more objects of other classes as an Instance variable. Here, by using the class name or by creating the object we can access the members of one class inside another class. It enables creating complex types by combining objects of different classes. It means that a class Composite can contain an object of another class Component. This type of relationship is known as Has-A Relation.

composition – diagrammatic representation

 In the above figure Classes are represented as boxes with the class name Composite and Component representing Has-A relation between both of them.   

class A :

      # variables of class A
      # methods of class A
      ...
      ...

class B : 
      # by using "obj" we can access member's of class A.
      obj = A()

      # variables of class B
      # methods of class B
      
      ...
      ...

Example : 

Python3




class Component:
  
   # composite class constructor
    def __init__(self):
        print('Component class object created...')
  
    # composite class instance method
    def m1(self):
        print('Component class m1() method executed...')
  
  
class Composite:
  
    # composite class constructor
    def __init__(self):
  
        # creating object of component class
        self.obj1 = Component()
          
        print('Composite class object also created...')
  
     # composite class instance method
    def m2(self):
        
        print('Composite class m2() method executed...')
  
        # calling m1() method of component class
        self.obj1.m1()
  
  
# creating object of composite class
obj2 = Composite()
  
# calling m2() method of composite class
obj2.m2()


Output

Component class object created...
Composite class object also created...
Composite class m2() method executed...
Component class m1() method executed...

Explanation:

  • In the above example, we created two classes Composite and Component to show the Has-A Relation among them.
  • In the Component class, we have one constructor and an instance method m1().
  • Similarly, in Composite class, we have one constructor in which we created an object of Component Class. Whenever we create an object of Composite Class, the object of the Component class is automatically created.
  • Now in m2() method of Composite class we are calling m1() method of Component Class using instance variable obj1 in which reference of Component Class is stored.
  • Now, whenever we call m2() method of Composite Class, automatically m1() method of Component Class will be called.

Inheritance and Composition in Python

This article will compare and highlight the features of is-a relation and has-a relation in Python.

Similar Reads

What is Inheritance (Is-A Relation)?

It is a concept of Object-Oriented Programming. Inheritance is a mechanism that allows us to inherit all the properties from another class. The class from which the properties and functionalities are utilized is called the parent class (also called as Base Class). The class which uses the properties from another class is called as Child Class (also known as Derived class). Inheritance is also called an Is-A Relation....

What is Composition (Has-A Relation)?

...

Composition vs Inheritance

It is one of the fundamental concepts of Object-Oriented Programming. In this concept, we will describe a class that references to one or more objects of other classes as an Instance variable. Here, by using the class name or by creating the object we can access the members of one class inside another class. It enables creating complex types by combining objects of different classes. It means that a class Composite can contain an object of another class Component. This type of relationship is known as Has-A Relation....

Contact Us