Learn-Object_Oriented_programming-with-us

Learn By Topic:

          

Class-objects

          

Inheritance

          

Polymorphism

          

Encapsulation

          

Abstraction

          

          

Click here to toggle b/w normal and focus mode: NORMAL MODE

Welcome to Part-2 of Inheritance

Imagine youself working in a Car company, you're manager gave you a list of items for data entry . But you're too lazy to ente to define and type everything, to solve this isses We will be using Concept of inheritance to reduce the complexity and work.

Intially while looking at the all parts and data you noticed few inputs are for common for other items,you observed brand,model,price are common for cars, infotainment system and other components, so you sorted them in a category and made a super class with a name Basic_info and instock

class Basic_info: #Parent class 1
    def __init__(self,brand,model,price):
        self.brand=brand
        self.model=model
        self.price=price

class instock: #Parent class 2
    def __init__(self,avail,shipment):
        self.avail=avail
        self.shipment=shipment  

Then you started working on data entry for car which has the following inputs: brand,model,price,avail,shipment. So instead of creating another class you'll inherit parent class-1 and parent class-2. This is an example of multiple inheritance

# example for multiple inheritance --> inheriting more than one parent classes          
class Cars(Basic_info,instock): #inheriting Basic_info class,instock class
    def __init__(self,brand,model,price,avail,shipment): #declaring child class its own init
        Basic_info.__init__(self,brand,model,price)  #inheriting Basic_info attributes
        instock.__init__(self,avail,shipment)  #inheriting instock attributes

After cars list then you came across next list to enter that is the the list of different infotainment systems used in the cars. Here you observed inputs of infotainment system takes the following inputs:

brand,model,price,spclfeatures,warranty,rating,avail,shipment

Here you can observe majority attributes i.e is inputs are similar to the Cars class we created before. Here we can inherit Cars class to make use of those attributes.This is an example of multilevel inheritance

#example for multilevel inheritance --> inheriting child class as parent     
class infotainment(Cars): 
    def __init__(self,brand,model,price,spclfeatures,warranty,rating,avail,shipment):
        Basic_info.__init__(self,brand,model,price)
        instock.__init__(self,avail,shipment)
        self.spclfea=spclfeatures #intializing additional required attributes
        self.warrn=warranty
        self.rating=rating

In addition to attributes in cars class, we additionaly require special features, warranty and rating so we simply intialize them in this class.

Now you came to final list to enter , that is a list of paints used for cars. Here paints take the following inputs: brand,model,price,composition,nature.

excluding composition and nature, brand,model,price are common in basic_info class, so we can inherit basic_info class to accquire those properties, we can intialize composition,nature in the paints class itself. This is an example for heirarchical inheritance as well as single inheritance

# example for single inheritance --> inheriting a single parent class       
# example for heirarchical inheritance --> inheriting same parent class more than once        
class paints(Basic_info): 
    def __init__(self,brand,model,price,composition,nature):
        Basic_info.__init__(self,brand,model,price)
        self.compo=composition #intializing additional required attributes
        self.nature=nature

I hope you are through with the concepts of Class and objects we discussed earlier, Now lets create objects of our classes.

Car objects

car1=Cars("Ford","GT-06","$45,0000","not available","expected to arrive soon") #obj-1
car2=Cars("Ferrari","MX-330","$67,000","Available","Ready to ship in 3 days") #obj-2

Infotainments objects

Zebronics=infotainment("Zebronics","Zeb-Thunder","$25","50W output with dual stereo speaker","5 years","5 star","available","ships in 3 days") #obj-3
Bose=infotainment("Bose","BV-420","$50","80W bt portable speaker","10 years","4.7 star","out of stock","no units available for shipment") #obj-4

Paint objects

paintbrand1=paints("SherinWilliams","Red_BX-550","$600/litre","Al2O3-2H2O+Na2B4O7-10H2O","medium") #obj-5
paintbrand2=paints("PPG_INDUSTRIES","BlueWhite_GTX1650","$400/litre","FeSO4-7H2O+2PbO-PbO2","low") #obj-6

U can create 'x' no.of objects for a class, try with you're own objects for better understanding. Similarly we can create methods inside our class for printing the information.

The entire code tailored together looks like this:

I've added print methods for each class

''' Authored By B.SasiVatsal on Dec-7,2021'''

#example for all types of inheritance(self,multiple,multilevel,heirarchical)
class Basic_info: #Parent class 1
    def __init__(self,brand,model,price):
        self.brand=brand
        self.model=model
        self.price=price
    def printinfo(self):
        print("Car brand and model is",self.brand,self.model)
        print("price is",self.price)
class instock: #Parent class 2
    def __init__(self,avail,shipment):
        self.avail=avail
        self.shipment=shipment        
# example for multiple inheritance --> inheriting more than one parent classes          
class Cars(Basic_info,instock): #inheriting Basic_info class,instock class
    def __init__(self,brand,model,price,avail,shipment): #declaring child class its own init
        Basic_info.__init__(self,brand,model,price)  #inheriting Basic_info attributes
        instock.__init__(self,avail,shipment)  #inheriting instock attributes
    def printCinfo(self):
        print("Car brand and model is",self.brand,self.model)
        print("price is",self.price)    
        print(self.avail)
        print(self.shipment)
#example for multilevel inheritance --> inheriting child class as parent     
class infotainment(Cars): 
    def __init__(self,brand,model,price,spclfeatures,warranty,rating,avail,shipment):
        Basic_info.__init__(self,brand,model,price)
        instock.__init__(self,avail,shipment)
        self.spclfea=spclfeatures #intializing additional required attributes
        self.warrn=warranty
        self.rating=rating
    def printinfo_of_inoftainment(self):
        print("Accessory brand and model is:",self.brand,self.model)
        print("Price:",self.price)
        print("warranty period:",self.warrn)
        print("Rating:",self.rating)
        print(self.brand,self.model,"is special for",self.spclfea)
        print(self.avail,"-",self.shipment)

# example for single inheritance --> inheriting a single parent class       
# example for heirarchical inheritance --> inheriting same parent class more than once        
class paints(Basic_info): 
    def __init__(self,brand,model,price,composition,nature):
        Basic_info.__init__(self,brand,model,price)
        self.compo=composition #intializing additional required attributes
        self.nature=nature
    def printpaint(self):
        print("Paint manufacturer and model is:",self.brand,self.model)
        print("Price:",self.price)
        print("Chemical Composition:",self.compo)
        print("Evnironmental harmfulness lvl:",self.nature)

car1=Cars("Ford","GT-06","$45,0000","not available","expected to arrive soon") #obj-1
car2=Cars("Ferrari","MX-330","$67,000","Available","Ready to ship in 3 days") #obj-2
car1.printCinfo()
print("\n")
car2.printCinfo()
Zebronics=infotainment("Zebronics","Zeb-Thunder","$25","50W output with dual stereo speaker","5 years","5 star","available","ships in 3 days") #obj-3
Bose=infotainment("Bose","BV-420","$50","80W bt portable speaker","10 years","4.7 star","out of stock","no units available for shipment") #obj-4
print("\n")
Zebronics.printinfo_of_inoftainment()
print("\n")
Bose.printinfo_of_inoftainment()
paintbrand1=paints("SherinWilliams","Red_BX-550","$600/litre","Al2O3-2H2O+Na2B4O7-10H2O","medium") #obj-5
paintbrand2=paints("PPG_INDUSTRIES","BlueWhite_GTX1650","$400/litre","FeSO4-7H2O+2PbO-PbO2","low") #obj-6
print("\n")
paintbrand1.printpaint()
print("\n")
paintbrand2.printpaint()

Output:

Car brand and model is Ford GT-06
price is $45,0000
not available
expected to arrive soon


Car brand and model is Ferrari MX-330
price is $67,000
Available
Ready to ship in 3 days


Accessory brand and model is: Zebronics Zeb-Thunder
Price: $25
warranty period: 5 years
Rating: 5 star
Zebronics Zeb-Thunder is special for 50W output with dual stereo speaker
available - ships in 3 days


Accessory brand and model is: Bose BV-420
Price: $50
warranty period: 10 years
Rating: 4.7 star
Bose BV-420 is special for 80W bt portable speaker
out of stock - no units available for shipment


Paint manufacturer and model is: SherinWilliams Red_BX-550
Price: $600/litre
Chemical Composition: Al2O3-2H2O+Na2B4O7-10H2O
Evnironmental harmfulness lvl: medium


Paint manufacturer and model is: PPG_INDUSTRIES BlueWhite_GTX1650
Price: $400/litre
Chemical Composition: FeSO4-7H2O+2PbO-PbO2
Evnironmental harmfulness lvl: low


** Process exited - Return Code: 0 **
Press Enter to exit terminal

I hope u clearly understood inheritance, types of inheritance, now try writing on you're own with a problem statement, don't forget to use all 5 types of inheritance in you're code like i did. BEST OF LUCK.

note: u can also find source code in 'Codes_src' directory in repo.

Thank you. Peace ✌️.


Made with ❤️ by SasiVatsal and Mowli