python


Python Inheritance


Inheritance can be defined as deriving an another class from a class with all its methods and properties.
Types of class:

  1. Parent class
  2. Child class
Parent class

Parent class is the class from which the another class is being inherited.It is also called Base class.

Create Parent class

Parent class is very simple to create:

Example

#Student here is the parent class
class Student:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

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



x = Student("Neha", "Arora")
x.printname()
Output:
Neha Arora

In the above code, we create a class named Student with firstname and lastname properties and printname method.

Child class

Child class is the class which is being inherited from the other class.It is also called Derived class.

Syntax:
class child_class(parent_class):
pass

The above code is for inheriting the child class from parent class.

Example:
class Car(Vehicle):
pass            #pass keyword is used when you don't have any class properties and methods.

In the aboe code, we created Car, a child class from a parent class Vehicle.

The __init__() function in Inheritance

As we discussed about creating a child class which inherits the properties and methods from its parent class above.
Now, we have to add __init__() function in the class.

Example:

class Car(Vehicle):
    def__init__(self,name,color):     #__init__() function is called automatically when the class is going to create an object.
    self.name=name
    self.color=color

For keeping the inheritance of parent's class __init__() function, we will add parent's class __init__() function.
Let's take an example:

Example:
class Car(Vehicle):
def__init__(self,name,color):     #__init__() function is called automatically when the class is going to create an object.
    self.name=name
    self.color=color
    Vehicle.__init__(self,name,color)
The super() function

the super() function is very useful in terms of Inheritance
This function will help the child class to inherit all the properties and methods from its parent class.

Example:
class Car(Vehicle):
def__init__(self,name,color):     
    self.name=name
    self.color=color
    super().__init__(self,name,color)

Note:In super() function, there is no need to add parent class name.It will automatically inherit the methods and properties from its parent class.

Add properties and methods

Now you can add all the properties and methods you want in your child class.

Example:

class Vehicle:
  def __init__(self,company, color):
    self.company=company
    self.color = color

  def printtype(self):
    print(self.company,self.color)

class Car(Vehicle):
  def __init__(self, company, color,name):
    super().__init__(company,color)
    self.name=name
    
  def printinfo(self):
    print("This is", self.name, "of company", self.company,"in",self.color,"color.")
    
x = Car("BMW", "blue", 'X5')
x.printinfo()
Output:
This is X5 of company BMW in blue color.

The above code will represents you the basic form of Inheritance.

It uses Vehicle as a parent class and Car as a child class.We also used __init__()andsuper()function as we have seen the use of them above.