Python Inheritance

The Python is an object-oriented programming language. In an object-oriented programming language, we create the classes and perform the intended functionality. Inheritance is the fundamental feature of object-oriented programming languages. Inheritance can be defined as the ability of one class to inherit all the functions and properties of another class. Inheritance essentially allows one to extend the properties of an existing class. Inheritance is leveraged with the two main concepts:

  1. Parent class or base class
  2. Child class or derived class

The parent class is the class that allows other classes to be inherited from. It is also called the Base Class. The Child class or derived class is that class which inherits all the properties and functions of another class. Inheritance promotes several benefits, i.e., it is the representation of the real world inheritance phenomenon. It encourages the reusability of code. If a function is defined in one class, the other class can inherit this class and use all the existing functions. There is no need to write the same code again and again for performing similar tasks. Inheritance is applied at multiple levels. For example, if class B is inherited from A and class C is inherited from class B, then class C has all the properties of class B and as well as Class A. The syntax of inheritance is as follows:

Class ParentClass:
    Implementation details of the parent class
class ChildClass:
    Implementation details of the child class

Although the child class has access to all the features and functions of the parent class, however, it can add its new features and functions as well.

This article describes and covers all the aspects of Python inheritance.

Inheritance implementation in Python

Inheritance can be applied in numerous situations. Let’s assume you are building a software system for a university. It can be comprised of multiple stakeholders like students, teachers, staff, etc. So every person has a name, age, email, and other common properties. It is not necessary to declare all the properties in each class separately. We can make a person class, and all the stakeholders’ classes can inherit all the common properties and functions from the person class. In this case, there is no need to write the common properties in each class again and again. Similarly, we can consider an animal class. There are hundreds of types of animals in this world. All the animals eat, sleep, and have some species as well. This concept can also be implemented using inheritance.

Let’s consider the animal as a super class and implement the inheritance. In the below given example, we have created one animal class. The animal class is the parent class. Moreover, we have created the Dog, and Cat classes which inherits the properties, and functions of the animal class. The pass keyword is used in the child class, when we do not have to implement any extended functionality in the child class.

#creating parent class
class Animal:
    #initialization function
    #initilization the animal name,and specie type
    def __init__(self,name,specie_type):
        self.name=name
        self.specie_type=specie_type
    #a function to print the animal name
    def printname(self):
        print("The name of animal is:",self.name)
        #a function to print the animal specie type
    def printspecie(self):
        print("The type of specie is:",self.specie_type)
#creating the dog class as child class of animal class
class Dog(Animal):
    # no extension or modification
    pass
#now dog class have access to all the functions and properties of animal class
#creating the dog class object
dogObj= Dog("Dog","carnivore")
dogObj.printname()
dogObj.printspecie()
#creating the cat class as child class of animal class
class Cat(Animal):
   #the initilization function of cat class
   def __init__(self):
       #calling and using the animal class initilization function
       Animal.__init__(self,"cat","carnivorous mammal")
#now cat class have access to all the functions and properties of animal class
#creating the cat class object
catObj= Cat()
catObj.printname()
catObj.printspecie()

Output

The “Animal.__init__(self,”cat”,”carnivorous mammal”)” call’s the initialization function of the Animal class.

The super() function

The Python provides a built-in super() function to inherit all the properties and functions of the parent class. When we use the super() function, then there is no need to mention the name of the parent class as we did in “Animal.__init__(self,”cat”,”carnivorous mammal”)” but the super() function automatically points to the parent class. Let’s use the super function.

#creating parent class
class Animal:
    #initialization function
    #initilization the animal name,and specie type
    def __init__(self,name,specie_type):
        self.name=name
        self.specie_type=specie_type
    #a function to print the animal name
    def printname(self):
        print("The name of animal is:",self.name)
        #a function to print the animal specie type
    def printspecie(self):
        print("The type of specie is:",self.specie_type)
#creating the dog class as child class of animal class
class Dog(Animal):
    #using super() function
    def __init__(self, name, specie_type):
        super().__init__(name, specie_type)
#now dog class have access to all the functions and properties of animal class
#creating the dog class object
dogObj= Dog("Dog","carnivore")
dogObj.printname()
dogObj.printspecie()
#creating the cat class as child class of animal class
class Cat(Animal):
   #the initilization function of cat class
    #using super() function
    def __init__(self, name, specie_type):
        super().__init__(name, specie_type)
#now cat class have access to all the functions and properties of animal class
#creating the cat class object
catObj= Cat("cat","carnivorous mammal")
catObj.printname()
catObj.printspecie()

Output

Now let’s add some more functionalities in the child classes. Every class inherits the common properties and functions from the parent class, but the child class can have some extra class that is intended for this particular class. Now let’s create some extra properties and functions in the dog, and cat class.

#creating parent class
class Animal:
    #initialization function
    #initilization the animal name,and specie type
    def __init__(self,name,specie_type):
        self.name=name
        self.specie_type=specie_type
    #a function to print the animal name
    def printname(self):
        print("The name of animal is:",self.name)
        #a function to print the animal specie type
    def printspecie(self):
        print("The type of specie is:",self.specie_type)
#creating the dog class as child class of animal class
class Dog(Animal):
    #using super() function
    #pet name is newly added functionality
    def __init__(self, name, specie_type,pet_name):
        super().__init__(name, specie_type)
        self.pet_name=pet_name
    #creating a new function
    def printpetname(self):
        print("The pet name is:",self.pet_name)
#now dog class have access to all the functions and properties of animal class
#creating the dog class object
dogObj= Dog("Dog","carnivore","Max")
dogObj.printname()
dogObj.printspecie()
dogObj.printpetname()
#creating the cat class as child class of animal class
class Cat(Animal):
   #the initilization function of cat class
    #using super() function
    #adding food and pet_name properties
    def __init__(self, name, specie_type,food, pet_name):
        super().__init__(name, specie_type)
        self.food=food
        self.pet_name=pet_name
    #new function to access food info
    def printfood(self):
        print("The cat likes:", self.food)
    #new function for pet name
    def printpetname(self):
        print("The pet name is:",self.pet_name)
#now cat class have access to all the functions and properties of animal class
#creating the cat class object
catObj= Cat("cat","carnivorous mammal","Biscuit","Daisy")
catObj.printname()
catObj.printspecie()

Output

The functions overriding

The function overriding is the important concept in inheritance. A function is called an overridden function if the name of the function is same in parent and child classes but the implementation or functionality of the function is different in each class. Let’s see the example of overridden function in the Animal class. In the below given example, we have an eat function in the animal classes and in its child classes (Dog, and Cat) as well. The name of the function is same in the classes but the implementation is different.

#creating parent class
class Animal:
   def eat(self):
       print("All the animals eat food")
#creating object
animalObj = Animal()
#calling function
animalObj.eat()
#creating dog class
class Dog(Animal):
    def eat(self):
        print("The dog eats meat")
#creating object
dogObj=Dog()
#calling function
dogObj.eat()
class Cat(Animal):
    def eat(self):
        print("The cat eat biscuits and some special food")
#creating object
catObj = Cat()
#calling function
catObj.eat()

Output

Conclusion

Inheritance is one of the fundamental concepts of object-oriented programming languages. By using inheritance, we create the child classes which inherit all the functions and properties of the parent class. Inheritance promotes the code re-usability. This article explains the inheritance in Python with examples.



from Linux Hint https://ift.tt/3myyrMM

Post a Comment

0 Comments