python39
python39
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
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
All these products Electronics, Kids Wear, Grocery etc.. have few common
attributes & methods.
Also, each product has specific attributes & methods of its own.
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
Reusability
Clear Separation
More Organized
Inheritance
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
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
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
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
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