Python Inheritance | How to use inheritance in python in easy way

Python Inheritance 

Inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in other class. 

In Python, the class which inherits the members of another class is called derived class and the class whose members are inherited is called a base class

The derived class is the specialized class for the base class. The class whose properties are inherited by other class is called the Parent or Base or Superclass

And, the class which inherits properties of other class is called Child or Derived or Subclass

Inheritance makes the code reusable. When we inherit an existing class, all its methods and fields become available in the new class, hence code is reused.


Create a Parent Class

Any class can be a Parent Class.
The syntax of the Parent Class is the same as the other Class.

Creating the Class named Student, with firstname and lastname properties, and a printstudent method:

Example:

class Student:
    def __init__(self,firstn,lastn):
        self.firstname = firstn
        self.lastname = lastn

    def printstudent(self):
        print(self.firstname,self.lastname)

student_input = Student(input("Enter First Name: "),input("Enter Second Name: "))
student_input.printstudent()

Output:

Enter First Name: Ankur
Enter Second Name: Ranpariya
Ankur Ranpariya


Types Of Inheritance 

  1. Single inheritance 
  2. Multiple inheritances 
  3. Hierarchical inheritance 
  4. Multilevel inheritance 
  5. Hybrid inheritance


Single Inheritance 

python inheritance


In single inheritance, there is only one base class and one derived class. The Derived class gets inherited from its base class. This is the simplest form of inheritance. The above figure is the diagram for single inheritance. 

Example:

class A:
    def a(self):
        print ("Class A")
class B(A):
    def b(self):
        print ("Class B")

objb=B()
objb.a()
objb.b()

Output:

Class A
Class B


Multiple Inheritance

python

In this type of inheritance, a single derived class may inherit from two or more base classes. above figures are the structure of Multiple Inheritance.

Example:

class A:
    def a(self):
        print ("Class A")
class B():
    def b(self):
        print ("Class B")
class C(A,B):
    def c(self):
        print ("Class C")

objc=C()
objc.a()
objc.b()
objc.c()

Output:

Class A
Class B
Class C

Multilevel Inheritance

The classes can also be derived from the classes that are already derived. This type of inheritance is called multilevel inheritance. above figures are the structure of Multilevel Inheritance.

Example:

class A:
    def a(self):
        print ("Class A")
class B(A):
    def b(self):
        print ("Class B")
class C(B):
    def c(self):
        print ("Class C")

objb=B()
objb.a()
objb.b()
objc=C()
objc.a()
objc.b()
objc.c()

Output:

Class A
Class B
Class A
Class B
Class C


Hierarchical Inheritance

In this type of inheritance, multiple derived classes get inherited from a single base class. The above figures are the structure of Hierarchical Inheritance.

Example:

class A:
    def a(self):
        print ("Class A")
class B(A):
    def b(self):
        print ("Class B")
class C(A):
    def c(self):
        print ("Class C")

objb=B()
objb.a()
objb.b()
objc=C()
objc.a()
objc.c()

Output:

Class A
Class B
Class A
Class C

 


Hybrid Inheritance

This is a Mixture of two or More Inheritance and in this Inheritance, a Code May Contains two or Three types of inheritance in a Single Code. The above figure is the diagram for Hybrid inheritance.

Example:

class School:
     def func1(self):
         print("This function is in school.")
  
class Student1(School):
     def func2(self):
         print("This function is in student 1. ")
  
class Student2(School):
     def func3(self):
         print("This function is in student 2.")
  
class Student3(Student1School):
     def func4(self):
         print("This function is in student 3.")

object = Student3()
object.func1()
object.func2()

Output:

This function is in school.
This function is in student 1.


Next Post Previous Post
No Comment
Add Comment
comment url