Python Inheritance – Linux Hint

Python is an object-oriented programming language. In an object-oriented programming language, we create classes and execute the expected functionalities. Inheritance is a 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 makes it possible to extend the properties of an existing class. Inheritance is used by two basic concepts:

  1. Parent or basic course
  2. Child class or derived class

A parent class is a class that makes it possible to inherit other classes. It is also called the base class. A descendant or derived class is a class that inherits all the characteristics and functions of another class. Inheritance has several advantages, i.e. it represents the real phenomenon of inheritance. It stimulates the reuse of the code. If a function is defined in a class, another class can inherit this class and use all existing functions. It is not necessary to write the same code over and over again to perform similar tasks. Inheritance occurs on different levels. If, for example, class B is inherited from A and class C is inherited from class B, then class C has all the characteristics of both class B and class A. The syntax of the inheritance is as follows:

Parent class:
Details on the implementation of the parental class ChildClass
:
Details of the execution of the children’s class

Although the subclass has access to all the features and functions of the parent class, it can add its own new features and functions.

This article describes and discusses all aspects of the heritage of the Pythonian language.

Inheritance to Python

Inheritance can be used in many situations. Suppose you’re building a software system for the university. It can consist of different interest groups, such as students, teachers, staff, etc. For example, each person has a name, age, email address and other common characteristics. It is not necessary to report all properties in each class separately. We can form a class of persons, and the classes of all interested parties can inherit all the common characteristics and functions of a class of persons. In this case, it is not necessary to write the common traits over and over again in each class. In the same way, we can look at the class of animals. There are hundreds of animal species in the world. All animals eat, sleep and possess certain species. This concept can also be realized through inheritance.

Let’s look at the animal as a superclass and realize its legacy. In the following example, we have created an animal class. The animal class is the class of the parents. We have also created classes of dogs and cats that inherit the characteristics and functions of the animal class. The keyword is used in the children’s class if we do not need to implement extensive functionality in the children’s class.

#Creation of parental class
of the animal class :
#Initialize function
#Initialize animal name and type
def __init__(self,name,specie_type):
self.name=name
self.specie_type=specie_type
# Print function for animal names
def print name (self):
print name (Tier name:,self.name)
# Print function for animal names
def print data (self):
print (Type:,self).specie_type)
#Create a dog class as a subclass of an animal
class dog(animal):
# without extension and modification
pass
# the white dog class has access to all functions and properties of the animal class
#Create a dog class object
dogObj= dog(dog,carnivore)
dogObj.printname()
dogObj.printspecie()
#Create the class of the dog as a child class of the animal
class Cat(Animal) :
#Kat class initialization function
def __init__(self) :
# Calls and uses the function to initialize the animal of class
.__init__(self,cat,carnivorous mammal)
# The white cat class has access to all functions and properties of the animal class
# Creates an object of the animal class
catObj= Cat()
catObj.printname()
catObj.printspecie()

Get off.

Call Animal.__init__(self, cat, carnivorous mammal) is a function of the initialization of the animal class.

Function super()

Python offers a built-in super() function to inherit all properties and functions of a parent class. When using the super() function, it is not necessary to give 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.

#The animal of parentage class
:
#Initialization function
#Initialize animal name and type
def __init__(self,name,specie_type):
self.name=name
self.specie_type=specie_type
#House name imprint function
def imprint name(self):
imprint(pet name:,self,specie_type).name)
#A pet name imprint function
def imprint mortar(self):
imprint(type:,self.specie_type)
#Create a dog class as a child class of an animal
dog class(pet):
#use super() function
def __init__(self, name, specie_type):
super().__init__(self, name, specie_type)
#Now the dog class has access to all functions and properties of the animal class
#Create the object of the dog class
dogObj= dog(dog,predator)
dogObj.printname()
dogObj.printspecie()
#Create the cat class as a subclass of an animal class
cat(animal) :
# Initializing the chat class
# Using the super() function
def __init__(self, name, specie_type):
super().__init__(self, name, specie_type)
# Now the cat class has access to all functions and properties of the animal class
# Creating the cat class object
catObj= cat(cat,carnivorous mammal)
catObj.printname()
catObj.printname()

Get off.

We now want to add some extras to the lessons for the children. Each class inherits the common features and functions of a parent class, but a child class may have an extra class dedicated to that particular class. Now let’s create some extra features in the category dogs and cats.

#Creation of parental class
of the animal class :
#Initialization function
#Initialize the name and type of the animal
def __init__(self,name,specie_type):
self.name=name
self.specie_type=specie_type
#A function to print the name of the animal
def print name(self):
print(Tiername:,self.
#A function to print the type of animal
def printpecie(self):
print(The type of animal is:,self.specie_type)
# Create a dog class as a child class of the animal
class dog(animal)
# Use the super() function
# carpet name is a new function
def __init__(self, name, specie_type,pet_name):
super().__init__(name, specie_type)
self.pet_name=pet_name
# Create a new function
def printpetname(self):
print(petname:,self.pet_name)
# the new class Dog has access to all functions and properties of the class pet
# Create the class object Dog
dogObj= Dog(Dog,Predator,Max)
dogObj.printname()
dogObj.printspecie()
dogObj9 dogObj.printpetname()
# Creating a cat class as a subclass of the animal class
cat(Animal) :
#Kat class initialization function
#Use of the super() function
#Additional feed materials and animal names
def __init__(self, name, species, feed, animal name):
super().__init__(self, name, specie_type)
self.food=food
self.pet_name=pet_name
#New function to access food information
def print food(self):
print(Cat likes:, self.food)
#New function for the name of the animal
def printpetname(self):
print(The name of the animal is:,self.pet_name)
#The cat class now has access to all functions and properties of the animal class
#Create the object of the cat class
catObj= cat(cat,carnivorous mammal,cookie,daisy)
catObj.printname()
catObj.printspecie()

Get off.

Overlapping functions

An important concept in the inheritance is the redefinition of functions. It is said that a function is overwritten if the name of the function in the parent and child class is the same, but the implementation or functionality of the function in each class is different. Let’s look at an example of a function that has been surpassed in the animal class. In the following example we have the nutritional function in both the classes for animals and the classes for children (dog, cat). The name of the function in the classes is the same, but the execution is different.

#Create parent class
class Animal:
def eat(self):
print(All animals eat food)
#Create object
animalObj = Animal()
#Call function
animalObj.eat()
#Create object
class Dog(Animal):
def eat(self):
print(All animals eat food)
#Create object
dogObj=Dog()
#Call function
dogObj.eat()
class Cat(Animal) :
def eat(self) :
print(The cat eat cookies and special food)
#Create object
catObj = Cat()
#Call function
catObj.eat()

Get off.

Conclusion

Compliance is one of the basic concepts of object-oriented programming languages. Using inheritance, we create subclasses that inherit all the functions and characteristics of the parent class. Inheritance promotes the reuse of the code. This article explains inheritance in Python using examples.

Related Tags:

python type() function,vscode python type checking,python runtime type checking,python3.6 static typing,python uses duck typing,python mypy,hybrid inheritance in python,python inheritance exercises,multilevel inheritance in python example,polymorphism in python journaldev,example of polymorphism in python,inheritance python terminology,idle python,how to open python idle in windows 10,pip install idle,how did python get its name,spyder software,wing ide