What is Inheritance in class ? : explained with example programs

     Python class inheritance 

The word inheritance means :

1: The act of inheriting property. 

2: The reception of genetic qualities by transmission from parent to offspring. 

3: The acquisition of a possession, condition, or trait from past generations.


Similarly in python Inheritance allows us to define a class that inherits all the methods and properties from another class.


Parent class: is the class being inherited from, also called a base class.Child class: is the class that inherits from another class, also called derived class.

Example :

 First we have to create a parent class, any class can be a parent class.

 

class Animal(object):
def speak(self):
print("speaking")

def speak_twice(self):
self.speak()
self.speak()

class Dog(Animal):
def speak(self):
print("woff")

class Cat(Animal):
def speak(self):
print("meow")
 
Here the class Animal is parent class, Dog and Cat are ,child class.
As Dog and Cat comes under the category Animal,The objects /Characters 
Of animal class can be used on any animal.
like all animal will have some common factors like speak,eating,sleeping ..
these will be created as objects of Animal class .And while creating child class 
it will be having specific qualities of that  particular species .like monkey have 
an extra object/character as it can climb tree. so the child class Monkey will be 
having this species specific quality/feature/object mentioned .    
 

Benefits of Inheritance:

Inheritance helps in code reuse. The child class may use the code defined in the 

parent class without re-writing it. Inheritance can save time and effort as the main

code need not be written again. Inheritance provides a clear model structure 

which is easy to understand. 

 

How to add  __init__() Function to child class?

Points to note:

-The _init_() function will be called automatically every-time we create a new object.

-The child's __init__() function overrides the inheritance of the parent's __init__() function.

i.e  when we directly  add __init__() in in our child class it will bypass the the __init__() of parent class , so the child class won't be getting data /values of parent class __init__().

To avoid this situation we have to  add a call to the parent's __init__() function.

Example:

#---------parent class------------------
class Animal():
def __init__(self,name ,type_) :
self.name =name
self.type=type_

def speak(self):
print("the animal started speaking")

def speak_twice(self):
self.speak()
self.speak()

#--------Child class-----------

class Dog(Animal):
#these are specific for this child/dog
def __init__(self, dog_breed,name, type_):
#these are specific for this child/dog
#which is not available in parent class
self.dog_breed=dog_breed
Animal.__init__(self,name,type_)
#---its possible to add objects particular to child class also----
def dog_jump(self):
print("jumping like a dog ")


c=Dog("labrador","loki","family dog")
print(c.dog_breed)
print(c.name)

c.speak_twice()
c.dog_jump()
 


Inheritance Using super() Function:

Python also has a super() function that will make the child class inherit all the methods and properties from its parent

By using the super() function, you do not have to use the name of the parent element, it will automatically inherit the methods and properties from its parent

 

Example:

#---------parent class------------------
class Animal():
def __init__(self,name ,type_) :
self.name =name
self.type=type_

def speak(self):
print("the animal started speaking")

def speak_twice(self):
self.speak()
self.speak()

#--------Child class-----------

class Dog(Animal):
#these are specific for this child/dog
def __init__(self, dog_breed,name, type_):
#these are specific for this child/dog
#which is not available in parent class
self.dog_breed=dog_breed
super().__init__(name,type_)
#---its possible to add objects particular to child class also----
def dog_jump(self):
print("jumping like a dog ")


c=Dog("labrador","loki","family dog")
print(c.dog_breed)
print(c.name)

c.speak_twice()
c.dog_jump()