Inheritance Concepts in python with example

By

There are some important points to define inheritance concepts in python.

  • Inheritance is the most important concept of object-oriented programming  language.It simulates the real world concepts of inheritance.
  • In inheritance, a new class called derived class can be created to inherit features of an existing class called base class.
  • Base class is also known as super class or parent class.
  • Derived class is also know as sub class or child class.
  • In inheritance,when we create the object of child class.Then this child class object acquires all the properties and behaviors of the parent object. 
  • Inheritance concept provide the re-usability of code.
Syntax:

class derived(base):
......Statements.......
Types of Inheritance 
There are basically three types of inheritance in python.
  1. General or Single Inheritance
  2. Multi-level Inheritance
  3. Multiple Inheritance
     1.) Single Inheritance:- A class can inherit single class inside the brackets,is known as single inheritance.We generally use single inheritance in our programs.
e.g.
#Base class
class man:
    def speak(self):
        print("This man is singing.")
    def color(self):
        print("This man color is white.")
    def height(self):
        print("This man height is 6 fit.")
#derived or child class
class son(man):
     def education(self):
         print("This son is post graduate degree.")
#creating the object of child class
s=son()
s.speak()
s.color()
s.education()
s.height()
Output:-
This man is singing.
This man color is white.
This son is post graduate degree.
This man height is 6 fit.
>>> 
     2.)  Multi-level Inheritance:- Multi-level inheritance is possible in python like other object oriented programming language(c++,java,c# etc.).Multiple inheritance is achieved when a derived class inherits the features of another derived class  upto more than one level.There are no limits of number of levels in python.You can use any number of level for multi-level inheritance.
Syntax:-

class class 1:
-------Statements------------
class class 2(class1):
-------Statements------------
class class 3(class 2):
-------Statements------------
.
.
.
.
class class N(CLASS N-1):
-------Statements------------
e.g.
#Base class
class Grandfather:
    def speak(self):
        print("Grand father speakes true.")
    def color(self):
        print("Grand Father color is white.")
    def thought(self):
        print("Grand father behaviour is simple and cool")
#derived or child class 1
class Father(Grandfather):
     def education(self):
         print("My Father is a B.Tech graduate")
#derived or child class 2
class son(Father):
    def height(self):
        print("This son height is 6 fit.")
#creating the object of child class
s=son()
s.thought()
s.color()
s.education()
s.height()
Output:-
Grand father behaviour is simple and cool
Grand Father color is white.
My Father is a B.Tech graduate
This son height is 6 fit.
>>> 
     3.) Multiple Inheritance:-

Python Provides the flexibility to inherit multiple base classes in the child class.

Syntax:-
class base1:
-------statements-------
class base2:
-------statements-------
class base3:
-------statements-------
.
.
.
.
class baseN:
-------statements-------

class derived(base1,base2,base3,...baseN):
-------statements-------
e.g.
#Creating the Base classes
class operation1:
   def sum(self,a,b):
       return a+b
class operation2:
   def substract(self,a,b):
       return a-b
class operation3:
   def division(self,a,b):
       return a/b
class operation4:
   def multiplication(self,a,b):
       return a*b    
#creating derived or child class 
class calculate(operation1,operation2,operation3,operation4):
    def mod(self,a,b):
        return a%b
#creating the object of child class
c=calculate()
a=int(input("Enter the first number: "))
b=int(input("Enter the second number: "))
print("The Sum of two number(a+b)= ",c.sum(a,b))
print("The Subtraction of two number(a-b)= ",c.substract(a,b))
print("The Division of two number(a/b)= ",c.division(a,b))
print("The Multiplication of two number(a*b)= ",c.multiplication(a,b))
print("The Modulous of two number(a%b)= ",c.mod(a,b))
Output:-
Enter the first number: 20
Enter the second number: 10
The Sum of two number(a+b)=  30
The Subtraction of two number(a-b)=  10
The Division of two number(a/b)=  2.0
The Multiplication of two number(a*b)=  200
The Modulous of two number(a%b)=  0
>>> 
For More...

0 comments:

Post a comment

Powered by Blogger.