PREV NEXT
Example 6:
Inheritance is divided 5 types, They are
- Single or Simple Inheritance
- Multiple Inheritances
- Multi Level Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
1. Single or Simple Inheritance:
In this type of inheritance only one level of inheritance available, it means one base/parent class and one derive/child class
Below is the example for single or simple inheritance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
class Person: name = "" age = 0 def __init__(self, personName, personAge): self.name = personName self.age = personAge def display(self): print("Name is:",self.name) print("Age is:",self.age) class Student(Person): studentId = "" fees=0.0 def __init__(self, studentName, studentAge, studentId,fees): Person.__init__(self, studentName, studentAge) self.studentId = studentId self.fees = fees def getId(self): return self.studentId def getFee(self): return self.fees print("Person Details are") print("******************") person = Person("Ricky Ponting", 50) person.display() print("******************") print("Student Details are") print("******************") student = Student("Ganguly", 50, "129",50000) student.display() print("Student Id:",student.getId()) print("Student Fee:",student.getFee()) print("******************") |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Person Details are ****************** Name is: Ricky Ponting Age is: 50 ****************** Student Details are ****************** Name is: Ganguly Age is: 50 Student Id: 129 Student Fee: 50000 ****************** |
2. Multiple Inheritance:
In this type of Inheritance one derive class created using more than on base class
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
class g_Father(): surName=" " habbits=" " attitude=" " def __init__(self,surname,habbits,attitude): self.surName=surname self.habbits=habbits self.attitude=attitude class father(): fname=" " color=" " skill=" " def __init__(self,fname,color,skill): self.fname=fname self.color=color self.skill=skill class son(g_Father,father): name= " " kind=" " bold= " " def __init__(self,surname,habbits,attitude,fname,color,skill,kind,bold): g_Father.__init__(self,surname,habbits,attitude,) father.__init__(self,fname,color,skill) self.name=self.surName+self.fname self.kind=kind self.bold=bold def display(self): print("Son Name is:",self.name) print("Son Kind is:",self.kind) print("Son Bold is:",self.bold) print("Son Habbits are:",self.habbits) print("Son Attitude is:",self.attitude) print("Son Color is:",self.color) print("Son Skill is:",self.skill) s=son("Sandhinti","cricket,reading","good"," Raja Shaker","White","Talentade","Honest","False") s.display() |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Son Name is: Sandhinti Raja Shaker Son Kind is: Honest Son Bold is: False Son Habbits are: cricket,reading Son Attitude is: good Son Color is: White Son Skill is: Talentade |
3. Multilevel Inheritance:
In this type of Inheritance more than one derived class created by using one base class
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
class Animal(): def __init__(self,eat,sleep): self.eat=eat self.sleep=sleep class Cat(Animal): def __init__(self,eat,sleep,bark,active): Animal.__init__(self,eat,sleep) self.bark=bark self.active=active class ChildCat(Cat): def __init__(self,eat,sleep,bark,active,attitude): Cat.__init__(self,eat,sleep,bark,active) self.attitude=attitude def display(self): print("ChildCat is eating:",self.eat) print("ChildCat is sleeping:",self.sleep) print("ChildCat is barking:",self.bark) print("ChildCat is Active:",self.active) print("ChildCat Attitude is:",self.attitude) bc=ChildCat("Very Fast","On Time","Very Loud",True,"Cunning") bc.display() |
Output:
1 2 3 4 5 6 7 8 9 |
ChildCat is eating: Very Fast ChildCat is sleeping: On Time ChildCat is barking: Very Loud ChildCat is Active: True ChildCat Attitude is: Cunning |
4. Hierachical Inheritance:
In this type of inheritance from one base class more than one derived class is derived.
5. Hybrid Inheritance:
Hybrid Inheritance is a combination of Multiple and Hierarchical Inheritance.
Overriding Methods:
Having same function name and function arguments in base class and as well as in derive class ,then we can call it as Overridden Methods.It is only possible in Inheritance.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Base(): def display(self): print("Base Class Display") class Derive(Base): def display(self): print("Derive Class Display") obj=Derive() obj.display() obj=Base() obj.display() |
Output:
1 2 3 |
Derive Class Display Base Class Display |
Example 7:
Encapsulation:
- Wrapping of data and method,method should work on that data is called Encapsulation,i.e capsules.
- By using Encapsulation we can achieve the data protections,it means nobody can access the data from the outside of the class,It will restrict to access the data and methods.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Bike: def __init__(self): self.__max_speed = 250 self.__name = "Yamaha-FZ-S" def drive(self): print ('driving maxspeed is dangerous ' + str(self.__max_speed)) bike= Bike() bike.drive() bike.__max_speed = 10 # If we try to change the value it will not Modify bike.drive() |
Output:
1 2 3 |
driving maxspeed is dangerous 250 driving maxspeed is dangerous 250 |
Note:
If we want change the data then we need to new method to set that data
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<strong>class Bike:</strong> def __init__(self): self.__max_speed = 250 self.__name = "Yamaha-FZ-S" def drive(self): print ('driving maxspeed is dangerous ' + str(self.__max_speed)) def setSpeed(self,speed): self.__max_speed = speed bike= Bike() bike.drive() bike.__max_speed = 10 # If we try to change the value it will not Modify bike.drive() bike.setSpeed(900) bike.drive() |
Output:
1 2 3 4 5 |
driving maxspeed is dangerous 250 driving maxspeed is dangerous 250 driving maxspeed is dangerous 900 |