7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=de3c24f3-… 1/7
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave
Inheritance - Part 2
How would you design and implement placing order with the
details of all the products bought?
Composition
Modelling instances of one class as attributes of another class is called
Composition
Code
PYTHON
1 class Product:
2
3 def __init__(self, name, price, deal_price, rat
4 self.name = name
5 self.price = price
6 self.deal_price = deal_price
7 self.ratings = ratings
8 self.you_save = price - deal_price
9
10 def display_product_details(self):
11 print("Product: {}".format(self.name))
Expand
Output
Product: Milk
Price: 40
Deal Price: 25
You Saved: 15
Ratings: 3.5
Quantity: 2
Product: TV
Price: 45000
Deal Price: 40000
You Saved: 5000
Expand
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=de3c24f3-… 2/7
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave
In the above example, we are modelling Product as attribute of Order
Overriding Methods
Sometimes, we require a method in the instances of a sub class to behave
differently from the method in instance of a superclass.
Code
PYTHON
1 class Product:
2
3 def __init__(self, name, price, deal_price, rating
4 self.name = name
5 self.price = price
6 self.deal_price = deal_price
7 self.ratings = ratings
8 self.you_save = price - deal_price
9
10 def display_product_details(self):
11 print("Product: {}".format(self.name))
Expand
Output
RecursionError: maximum recursion depth exceeded
Because
self.display_product_details() in ElectronicItem class does not call the
method in the superclass.
Super
Accessing Super Class’s Method
super() allows us to call methods of the superclass (Product) from the
subclass.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=de3c24f3-… 3/7
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave
Instead of writing and methods to access and modify warranty we can
override
__init__
Let's add warranty of ElectronicItem.
Code
PYTHON
1 class Product:
2
3 def __init__(self, name, price, deal_price, rating
4 self.name = name
5 self.price = price
6 self.deal_price = deal_price
7 self.ratings = ratings
8 self.you_save = price - deal_price
9
10 def display_product_details(self):
11 print("Product: {}".format(self.name))
Expand
Output
Product: Laptop
Price: 45000
Deal Price: 40000
You Saved: 5000
Ratings: 3.5
Warranty 10 months
MultiLevel Inheritance
We can also inherit from a subclass. This is called MultiLevel
Inheritance.
We can continue such inheritance to any depth in Python.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=de3c24f3-… 4/7
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave
PYTHON
1 class Product:
2 pass
3
4 class ElectronicItem(Product):
5 pass
6
7 class Laptop(ElectronicItem):
8 pass
Inheritance & Composition
When to use Inheritance?
Prefer modeling with inheritance when the classes have an IS-A
relationship.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=de3c24f3-… 5/7
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave
When to use Composition?
Prefer modeling with composition when the classes have a HAS-A
relationship.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=de3c24f3-… 6/7
7/27/24, 9:52 PM Revolutionizing the Job Market | NxtWave
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=de3c24f3-… 7/7