Python Inheritance

 

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.

Common Attributes & Methods

All these products Electronics, Kids Wear, Grocery etc.. have few common attributes & methods.

Specific Attributes & Methods

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

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.

Super Class

Code

class Product:
def __init__(self, name, price, deal_price, ratings):
self.name = name
self.price = price
self.deal_price = deal_price
self.ratings = ratings
self.you_save = price - deal_price
def display_product_details(self):
print("Product: {}".format(self.name))
print("Price: {}".format(self.price))
print("Deal Price: {}".format(self.deal_price))
print("You Saved: {}".format(self.you_save))
print("Ratings: {}".format(self.ratings))
p = Product("Shoes",500, 250, 3.5)
p.display_product_details()
PYTHON
Collapse

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

Code

class Product:
def __init__(self, name, price, deal_price, ratings):
self.name = name
self.price = price
self.deal_price = deal_price
self.ratings = ratings
self.you_save = price - deal_price
def display_product_details(self):
print("Product: {}".format(self.name))
print("Price: {}".format(self.price))
print("Deal Price: {}".format(self.deal_price))
print("You Saved: {}".format(self.you_save))
print("Ratings: {}".format(self.ratings))
class ElectronicItem(Product):
pass
class GroceryItem(Product):
pass
e = ElectronicItem("TV",45000, 40000, 3.5)
e.display_product_details()
PYTHON
Collapse

Output

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

Example 2

Code

class Product:
def __init__(self, name, price, deal_price, ratings):
self.name = name
self.price = price
self.deal_price = deal_price
self.ratings = ratings
self.you_save = price - deal_price
def display_product_details(self):
print("Product: {}".format(self.name))
print("Price: {}".format(self.price))
print("Deal Price: {}".format(self.deal_price))
print("You Saved: {}".format(self.you_save))
print("Ratings: {}".format(self.ratings))
class ElectronicItem(Product):
pass
class GroceryItem(Product):
pass
e = GroceryItem("milk", 25, 20, 3)
e.display_product_details()
PYTHON
Collapse

Output

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

Example 3

Code

class Product:
def __init__(self, name, price, deal_price, ratings):
self.name = name
self.price = price
self.deal_price = deal_price
self.ratings = ratings
self.you_save = price - deal_price
def display_product_details(self):
print("Product: {}".format(self.name))
print("Price: {}".format(self.price))
print("Deal Price: {}".format(self.deal_price))
print("You Saved: {}".format(self.you_save))
print("Ratings: {}".format(self.ratings))
class ElectronicItem(Product):
def set_warranty(self, warranty_in_months):
self.warranty_in_months = warranty_in_months
def get_warranty(self):
return self.warranty_in_months
e = ElectronicItem("TV",45000, 40000, 3.5)
e.set_warranty(24)
print(e.get_warranty())
PYTHON
Collapse

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

class Product:
def __init__(self, name, price, deal_price, ratings):
self.name = name
self.price = price
self.deal_price = deal_price
self.ratings = ratings
self.you_save = price - deal_price
def display_product_details(self):
print("Product: {}".format(self.name))
print("Price: {}".format(self.price))
print("Deal Price: {}".format(self.deal_price))
print("You Saved: {}".format(self.you_save))
print("Ratings: {}".format(self.ratings))
class ElectronicItem(Product):
def set_warranty(self, warranty_in_months):
self.warranty_in_months = warranty_in_months
def get_warranty(self):
return self.warranty_in_months
p = Product("TV",45000, 40000, 3.5)
p.set_warranty(24)
PYTHON
Collapse

Output

AttributeError: 'Product' object has no attribute 'set_warranty'

Sub Class Method

Code

class Product:
def __init__(self, name, price, deal_price, ratings):
self.name = name
self.price = price
self.deal_price = deal_price
self.ratings = ratings
self.you_save = price - deal_price
def display_product_details(self):
print("Product: {}".format(self.name))
print("Price: {}".format(self.price))
print("Deal Price: {}".format(self.deal_price))
print("You Saved: {}".format(self.you_save))
print("Ratings: {}".format(self.ratings))
class ElectronicItem(Product):
def set_warranty(self, warranty_in_months):
self.warranty_in_months = warranty_in_months
def get_warranty(self):
return self.warranty_in_months
e = ElectronicItem("TV",45000, 40000, 3.5)
e.set_warranty(24)
e.display_product_details()
PYTHON
Collapse

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

class Product:
def __init__(self, name, price, deal_price, ratings):
self.name = name
self.price = price
self.deal_price = deal_price
self.ratings = ratings
self.you_save = price - deal_price
def display_product_details(self):
print("Product: {}".format(self.name))
print("Price: {}".format(self.price))
print("Deal Price: {}".format(self.deal_price))
print("You Saved: {}".format(self.you_save))
print("Ratings: {}".format(self.ratings))
class ElectronicItem(Product):
def set_warranty(self, warranty_in_months):
self.warranty_in_months = warranty_in_months
def get_warranty(self):
return self.warranty_in_months
def display_electronic_product_details(self):
self.display_product_details()
print("Warranty {} months".format(self.warranty_in_months))
e = ElectronicItem("TV",45000, 40000, 3.5)
e.set_warranty(24)
e.display_electronic_product_details()
 
PYTHON
Collapse

Output

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

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form