- We can use members of one class inside another class by using the following ways.
- By Composition (Has-A Relationship)
- By Inheritance (IS-A Relationship)
By Composition (Has-A Relationship)
- By using class name or by creating object we can access members of one class inside another class is nothing but composition (Has-A Relationship).
- The main advantage of has-a relationship is code reusability.
Example
Python
class Engine:
a=10
def __init__(self):
self.b=20
def m1(self):
print('Engine Specific Functionality')
class Car:
def __init__(self):
self.engine=Engine()
def m2(self):
print('Car using Engine Class Functionality')
print(self.engine.a)
print(self.engine.b)
self.engine.m1()
c=Car()
c.m2()
Output
PowerShell
Car using Engine Class Functionality
10
20
Engine Specific Functionality
Example
Python
class Car:
def __init__(self,name,model,color):
self.name=name
self.model=model
self.color=color
def getinfo(self):
print("Car Name:{} , Model:{} and Color:{}".format(self.name,self.model,self.color))
class Employee:
def __init__(self,ename,eno,car):
self.ename=ename
self.eno=eno
self.car=car
def empinfo(self):
print("Employee Name:",self.ename)
print("Employee Number:",self.eno)
print("Employee Car Info:")
self.car.getinfo()
c=Car("Innova","2.5V","Grey")
e=Employee('Durga',10000,c)
e.empinfo()
Output
PowerShell
Employee Name: Durga
Employee Number: 10000
Employee Car Info:
Car Name: Innova, Model:2.5V and Color:Grey
In the above program Employee class Has-A Car reference and hence Employee class can access all
members of Car class.
Example
Python
class X:
a=10
def __init__(self):
self.b=20
def m1(self):
print("m1 method of X class")
class Y:
c=30
def __init__(self):
self.d=40
def m2(self):
print("m2 method of Y class")
def m3(self):
x1=X()
print(x1.a)
print(x1.b)
x1.m1()
print(Y.c)
print(self.d)
self.m2()
print("m3 method of Y class")
y1=Y()
y1.m3()
Output
PowerShell
10
20
m1 method of X class
30
40
m2 method of Y class
m3 method of Y class
By Inheritance(IS-A Relationship)
- Whatever variables, methods and constructors available in the parent class by default available to the child classes and we are not required to rewrite. Hence the main advantage of inheritance is code reusability and we can extend existing functionality with some more extra functionality.
Syntax
- class childclass(parentclass)
Example
Python
1) class P:
2) a=10
3) def __init__(self):
4) self.b=10
5) def m1(self):
6) print('Parent instance method')
7) @classmethod
8) def m2(cls):
9) print('Parent class method')
10) @staticmethod
11) def m3():
12) print('Parent static method')
13)
14) class C(P):
15) pass
16)
17) c=C()
18) print(c.a)
19) print(c.b)
20) c.m1()
21) c.m2()
22) c.m3()
Output
PowerShell
10
10
Parent instance method
Parent class method
Parent static method
Example
Python
class P:
10 methods
class C(P):
5 methods
Output
PowerShell
3
- In the above example parent class contains 10 methods and these methods automatically available to the child class and we are not required to rewrite those methods(code reusability) hence child class contains 15 methods.
Note
- What ever members present in parent class are by default available to the child class through inheritance.
Example
Python
class P:
def m1(self):
print("Parent class method")
class C(P):
def m2(self):
print("Child class method")
c=C();
c.m1()
c.m2()
Output
PowerShell
Parent class method
Child class method
- What ever methods present in parent class are automatically available to the child class and hence on the child class reference we can call both parent class methods and child class methods.
Example
- Similarly variables also.
Python
class P:
a=10
def __init__(self):
self.b=20
class C(P):
c=30
def __init__(self):
super().__init__()===>Line-1
self.d=30
c1=C()
print(c1.a,c1.b,c1.c,c1.d)
If we comment Line-1 then variable b is not available to the child class.
Output
PowerShell
6
Example
Python
class Person:
def __init__(self,name,age):
self.name=name
self.age=age
def eatndrink(self):
print('Eat Biryani and Drink Beer')
class Employee(Person):
def __init__(self,name,age,eno,esal):
super().__init__(name,age)
self.eno=eno
self.esal=esal
def work(self):
print("Coding Python is very easy just like drinking Chilled Beer")
def empinfo(self):
print("Employee Name:",self.name)
print("Employee Age:",self.age)
print("Employee Number:",self.eno)
print("Employee Salary:",self.esal)
e=Employee('Durga', 48, 100, 10000)
e.eatndrink()
e.work()
e.empinfo()
Output
PowerShell
Eat biryani and drink Beer
Coding python is very easy just like drinking chilled Beer
Employee Name: Durga
Employee Age: 48
Employee Number: 100
Employee Salary: 10000
IS-A vs HAS-A Relationship
- If we want to extend existing functionality with some more extra functionality then we should go for is-a relationship
- If we dont want to extend and just we have to use existing functionality then we should go for has-a relationship.

Example
Python
Employee class extends person class Functionality.
But Employee class just uses car functionality but not extending.
class Car:
def __init__(self,name,model,color):
self.name=name
self.model=model
self.color=color
def getinfo(self):
print("\tCar Name:{} \n\t Model:{} \n\t Color:{}".format(self.name,self.model,self.color))
class Person:
def __init__(self,name,age):
self.name=name
self.age=age
def eatndrink(self):
print('Eat Biryani and Drink Beer')
class Employee(Person):
def __init__(self,name,age,eno,esal,car):
super().__init__(name,age)
self.eno=eno
self.esal=esal
self.car=car
def work(self):
print("Coding Python is very easy just like drinking Chilled Beer")
def empinfo(self):
print("Employee Name:",self.name)
print("Employee Age:",self.age)
print("Employee Number:",self.eno)
print("Employee Salary:",self.esal)
print("Employee Car Info:")
self.car.getinfo()
c=Car("Innova","2.5V","Grey")
e=Employee('Durga',48,100,10000,c)
e.eatndrink()
e.work()
e.empinfo()
Output
PowerShell
Eat Biryani and Drink Beer
Coding Python is very easy just like drinking Chilled Beer
Employee Name: Durga
Employee Age: 48
Employee Number: 100
Employee Salary: 10000
Employee Car Info:
Car Name:Innova
Model:2.5V
Color:Grey
In the above example Employee class extends Person class functionality but just uses Car class
functionality.