0% found this document useful (0 votes)
5 views10 pages

E-commerce Product Modeling Guide

The document discusses the modeling of an e-commerce site with a focus on different product categories such as Electronics and Grocery. It explains the concepts of inheritance in object-oriented programming, showcasing how subclasses can inherit attributes and methods from a superclass. Additionally, it highlights the advantages of this modeling approach, including reusability and organization.

Uploaded by

Prachi More
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

E-commerce Product Modeling Guide

The document discusses the modeling of an e-commerce site with a focus on different product categories such as Electronics and Grocery. It explains the concepts of inheritance in object-oriented programming, showcasing how subclasses can inherit attributes and methods from a superclass. Additionally, it highlights the advantages of this modeling approach, including reusability and organization.

Uploaded by

Prachi More
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

7/27/24, 9:51 PM Revolutionizing the Job Market | NxtWave

[Link] 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.

[Link] 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


[Link] 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

[Link] 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.

[Link] 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 [Link] = name
4 [Link] = price
5 self.deal_price = deal_price
6 [Link] = ratings
7 self.you_save = price - deal_price
8 def display_product_details(self):
9 print("Product: {}".format([Link]))
10 print("Price: {}".format([Link]))
11 print("Deal Price: {}".format([Link] 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
[Link] 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 [Link] = name
4 [Link] = price
5 self.deal_price = deal_price
6 [Link] = ratings
7 self.you_save = price - deal_price
8 def display_product_details(self):
9 print("Product: {}".format([Link]))
10 print("Price: {}".format([Link]))
11 print("Deal Price: {}".format([Link] 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 [Link] = name
4 [Link] = price
5 self.deal_price = deal_price
6 [Link] = ratings
7 self.you_save = price - deal_price
8 def display_product_details(self):
9 print("Product: {}".format([Link]))
10 print("Price: {}".format([Link]))
11 print("Deal Price: {}".format([Link] pri
Expand

Output

Product: milk Price: 25 Deal Price: 20 You Saved: 5 Ratings: 3

Example 3

Code

[Link] 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 [Link] = name
4 [Link] = price
5 self.deal_price = deal_price
6 [Link] = ratings
7 self.you_save = price - deal_price
8 def display_product_details(self):
9 print("Product: {}".format([Link]))
10 print("Price: {}".format([Link]))
11 print("Deal Price: {}".format([Link] 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 [Link] = name
4 [Link] = price
5 self.deal_price = deal_price
6 [Link] = ratings
7 self.you_save = price - deal_price
8 def display_product_details(self):
9 print("Product: {}".format([Link]))
10 print("Price: {}".format([Link]))
11 print("Deal Price: {}".format([Link] pric
Expand

[Link] 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 [Link] = name
4 [Link] = price
5 self.deal_price = deal_price
6 [Link] = ratings
7 self.you_save = price - deal_price
8 def display_product_details(self):
9 print("Product: {}".format([Link]))
10 print("Price: {}".format([Link]))
11 print("Deal Price: {}".format([Link] 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 [Link] = name
4 [Link] = price
5 self.deal_price = deal_price
6 [Link] = ratings
7 self.you_save = price - deal_price
8 def display_product_details(self):
[Link] 9/10
7/27/24, 9:51 PM Revolutionizing the Job Market | NxtWave

9 print("Product: {}".format([Link]))
10 print("Price: {}".format([Link]))
11 print("Deal Price: {}".format([Link] price

Expand

Output

Product: TV Price: 45000 Deal Price: 40000 You Saved: 5000 Ratings: 3.5
Warranty 24 months

[Link] 10/10

You might also like