Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Implementation of Composition (Has-A Relation) in Python

  • Last Updated : 19 Apr, 2021

We can access the member of one class inside a class using these 2 concepts: 

  • By Composition(Has-A Relation)
  • By Inheritance(Is-A Relation)

Here we will study how to use implement Has-A Relation in Python.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Implementation of Composition in Python



By using the class names or by creating an object we can access the member of one class inside another class. 

Example:

class Engine:
   # engine specific functionality
   '''
   '''
   '''

class Car:
   e = Engine()
   e.method1()
   e.method2()
   '''
   '''

In these above example class Car has-A Engine class reference. Here inside class Car also we can create different variables and methods. Using object reference of Engine class inside Car class we can easily access each and every member of Engine class inside Car class.

Example 1: Executable code for composition

Python3




class Employee:
 
    # constructor for initialization
    def __init__(self, name, age):
        self.name = name
        self.age = age
 
    # instance method
    def emp_data(self):
        print('Name of Employee : ', self.name)
        print('Age of Employee : ', self.age)
 
 
class Data:
    def __init__(self, address, salary, emp_obj):
        self.address = address
        self.salary = salary
 
        # creating object of Employee class
        self.emp_obj = emp_obj
 
    # instance method
    def display(self):
 
        # calling Employee class emp_data()
        # method
        self.emp_obj.emp_data()
        print('Address of Employee : ', self.address)
        print('Salary of Employee : ', self.salary)
 
# creating Employee class object
emp = Employee('Ronil', 20)
 
# passing obj. of Emp. class during creation
# of Data class object
data = Data('Indore', 25000, emp)
 
# call Data class instance method
data.display()

 
 

Output
Name of Employee :  Ronil
Age of Employee :  20
Address of Employee :  Indore
Salary of Employee :  25000

 

Here in the above example, we have 2 classes ‘Employee’ and ‘Data’. Inside ‘Data’ class Constructor we are creating an object of Employee class due to which we can access the members of the Employee class easily. Inside the Data class Employee class object becomes an instance variable of “Data” class. We are creating objects inside the constructor so whenever we will call any method or variable of class Employee we must use self keyword. We can replace “self.emp_obj” to “Employee”, but by using the class name Employee we can access only the static method or variable of the Employee class.



 

Example 2: Another simple example using Composition

 

Python3




class A:
    def __init__(self):
        print('Class - A Constructor')
 
    def m1(self):
        print('M1 method of Class - A.')
 
 
class B:
    def __init__(self):
        print('Class - B Constructor.')
 
    # instance method
    def m2(self):
 
        # creating object of class A inside
        # B class instance method
        obj = A()
 
        # calling m1() method of class-A
        obj.m1()
        print('M2 method of Class - B.')
 
 
# creating object of class-B
obj = B()
 
# calling B class m2() method
obj.m2()

Output

Class - B Constructor.
Class - A Constructor
M1 method of Class - A.
M2 method of Class - B.

 

Here in the above example, we are creating an object of class A inside instance method of class B that is m2() method. So the flow of execution will be Initially, the object of class B will be created so the constructor of class B will get executed. Now object of class B is calling m2() method, so the cursor will go to the m2() method of class B. Inside the m2() method of class B object of class A is created so the constructor of class A will be executed then m1() method of class A will be executed finally it will print the final statement of m2() method and execution end’s here.

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :