7/27/24, 9:51 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=cb1b26a… 1/10
7/27/24, 9:51 PM Revolutionizing the Job Market | NxtWave
Inheritance
Products
Lets model e-commerce site having different products like Electronics, Kids
Wear, Grocery, etc.
Electronic Item
Following are few attributes & methods for an Electronic product.
Grocery Item
Similarly, attribute & methods for a Grocery item.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=cb1b26a… 2/10
7/27/24, 9:51 PM Revolutionizing the Job Market | NxtWave
Common Attributes & Methods
All these products Electronics, Kids Wear, Grocery etc.. have few common
attributes & methods.
Specific Attributes & Methods
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=cb1b26a… 3/10
7/27/24, 9:51 PM Revolutionizing the Job Market | NxtWave
Also, each product has specific attributes & methods of its own.
Electronic & Grocery Items
Electronic Item & Grocery Item will have all attributes & methods which are
common to all products.
Lets Separate the common attributes & methods as Product
Modelling Classes
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=cb1b26a… 4/10
7/27/24, 9:51 PM Revolutionizing the Job Market | NxtWave
Advantages of Modelling Classes as above
Reusability
Clear Separation
More Organized
Inheritance
Inheritance is a mechanism by which a class inherits attributes and methods
from another class.
With Inheritance, we can have
ElectronicItem inherit the attributes & methods from Product instead
of defining them again.
Product is Super/Base/Parent Class and ElectronicItem is Sub/Derived/Child
Class.
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=cb1b26a… 5/10
7/27/24, 9:51 PM Revolutionizing the Job Market | NxtWave
Super Class
Code
PYTHON
1 class Product:
2 def __init__(self, name, price, deal_price, rat
3 self.name = name
4 self.price = price
5 self.deal_price = deal_price
6 self.ratings = ratings
7 self.you_save = price - deal_price
8 def display_product_details(self):
9 print("Product: {}".format(self.name))
10 print("Price: {}".format(self.price))
11 print("Deal Price: {}".format(self.deal pri
Expand
Output
Product: Shoes Price: 500 Deal Price: 250 You Saved: 250 Ratings: 3.5
Sub Class
The subclass automatically inherits all the attributes & methods from its
superclass.
Example 1
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=cb1b26a… 6/10
7/27/24, 9:51 PM Revolutionizing the Job Market | NxtWave
Code
PYTHON
1 class Product:
2 def __init__(self, name, price, deal_price, rat
3 self.name = name
4 self.price = price
5 self.deal_price = deal_price
6 self.ratings = ratings
7 self.you_save = price - deal_price
8 def display_product_details(self):
9 print("Product: {}".format(self.name))
10 print("Price: {}".format(self.price))
11 print("Deal Price: {}".format(self.deal pri
Expand
Output
Product: TV Price: 45000 Deal Price: 40000 You Saved: 5000 Ratings: 3.5
Example 2
Code
PYTHON
1 class Product:
2 def __init__(self, name, price, deal_price, rat
3 self.name = name
4 self.price = price
5 self.deal_price = deal_price
6 self.ratings = ratings
7 self.you_save = price - deal_price
8 def display_product_details(self):
9 print("Product: {}".format(self.name))
10 print("Price: {}".format(self.price))
11 print("Deal Price: {}".format(self.deal pri
Expand
Output
Product: milk Price: 25 Deal Price: 20 You Saved: 5 Ratings: 3
Example 3
Code
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=cb1b26a… 7/10
7/27/24, 9:51 PM Revolutionizing the Job Market | NxtWave
PYTHON
1 class Product:
2 def __init__(self, name, price, deal_price, rati
3 self.name = name
4 self.price = price
5 self.deal_price = deal_price
6 self.ratings = ratings
7 self.you_save = price - deal_price
8 def display_product_details(self):
9 print("Product: {}".format(self.name))
10 print("Price: {}".format(self.price))
11 print("Deal Price: {}".format(self.deal pric
Expand
Output
24
In the above example, calling
set_warranty will create an attribute warranty_in_months .
Super Class & Sub Class
Superclass cannot access the methods and attributes of the subclass.
Code
PYTHON
1 class Product:
2 def __init__(self, name, price, deal_price, rati
3 self.name = name
4 self.price = price
5 self.deal_price = deal_price
6 self.ratings = ratings
7 self.you_save = price - deal_price
8 def display_product_details(self):
9 print("Product: {}".format(self.name))
10 print("Price: {}".format(self.price))
11 print("Deal Price: {}".format(self.deal pric
Expand
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=cb1b26a… 8/10
7/27/24, 9:51 PM Revolutionizing the Job Market | NxtWave
Output
AttributeError: 'Product' object has no attribute 's
Sub Class Method
Code
PYTHON
1 class Product:
2 def __init__(self, name, price, deal_price, rati
3 self.name = name
4 self.price = price
5 self.deal_price = deal_price
6 self.ratings = ratings
7 self.you_save = price - deal_price
8 def display_product_details(self):
9 print("Product: {}".format(self.name))
10 print("Price: {}".format(self.price))
11 print("Deal Price: {}".format(self.deal pric
Expand
Output
Product: TV Price: 45000 Deal Price: 40000 You Saved: 5000 Ratings: 3.5
Calling Super Class Method
We can call methods defined in superclass from the methods in the
subclass.
Code
PYTHON
1 class Product:
2 def __init__(self, name, price, deal_price, ratin
3 self.name = name
4 self.price = price
5 self.deal_price = deal_price
6 self.ratings = ratings
7 self.you_save = price - deal_price
8 def display_product_details(self):
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=cb1b26a… 9/10
7/27/24, 9:51 PM Revolutionizing the Job Market | NxtWave
9 print("Product: {}".format(self.name))
10 print("Price: {}".format(self.price))
11 print("Deal Price: {}".format(self.deal price
Expand
Output
Product: TV Price: 45000 Deal Price: 40000 You Saved: 5000 Ratings: 3.5
Warranty 24 months
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=b6025dfc-a6e3-4225-ad36-ad07ca8d9f4e&s_id=cb1b26… 10/10