Skip to main content

Python Inheritance


In this tutorial you will learn:

  • Inheritance in Object Oriented Programming
  • Inheritance in Python
  • Overriding in Python OOP
  • Private members of Parent Class

Inheritance in Object Oriented Programming

Inheritance is an important concept in Object Oriented Programming. Inheritance means getting something from the parent and in programming it means the same the child class inherits methods and properties from the parent class. It enables code reusability and resembles real world relationships.

Inheritance in Python

We can also apply inheritance in Python. The class from which we will inherit methods and properties is called Parent class and the class which inherits the methods and properties is called child class. In order to inherit from a class we need to write the parent class name in round brackets along with name of the child class.

Example:

Let's create an animal class with some properties. This will serve as the parent class as all animals will inherit properties from this parent class.

  1. class Animal:
  2. name = ""
  3. no_of_legs = 0
  4. sound_of_animal = ""
  5. def __init__(self, name, no_of_legs, sound_of_animal):
  6. self.name = name
  7. self.no_of_legs = no_of_legs
  8. self.sound_of_animal = sound_of_animal

Now after making the parent class we will create a child class that will inherit properties from the parent class and add some properties of its own.

  1. class Cat(Animal):
  2. __owner = ""
  3. def __init__(self, name, no_of_legs,sound_of_animal):
  4. super().__init__(name, no_of_legs, sound_of_animal)
  5. def setOwner(self, owner):
  6. self.__owner = owner
  7. def printDetails(self):
  8. print("Owner is", self.__owner)
  9. print("Cat name", self.name)
  10. print("No of Legs", self.no_of_legs)
  11. print("Sound of Cat", self.sound_of_animal)

This class adds a print details function and a private owner property. Details of private properties will be explained later in this tutorial. Notice the init function it calls the init function of the parent class with the keyword "super" this keyword is equivalent to calling the parent class with its name Animal.__init__. We want to initialize this function in order to set all the properties that are inherited by the child class from the parent class.

Overriding in Python OOP

Overriding in Python means extending the functionality of an existing function in the child class. Let's take the same example of Animal class and see how it can be changed if we use method overriding.

Example:

  1. class Animal:
  2. name = ""
  3. no_of_legs = 0
  4. sound_of_animal = ""
  5. def __init__(self, name, no_of_legs, sound_of_animal):
  6. self.name = name
  7. self.no_of_legs = no_of_legs
  8. self.sound_of_animal = sound_of_animal
  9. def printDetails(self):
  10. print("\n\nAnimal Name", self.name)
  11. print("No of Legs", self.no_of_legs)
  12. print("Sound of Animal", self.sound_of_animal)

Here we note that print details function is moved from child class to the parent class as in this way we can extend it's functionality in base class. We can add more statements as required in the base class. Let's have a look.

  1. class Cat(Animal):
  2. __owner = ""
  3. def __init__(self, name, no_of_legs,sound_of_animal):
  4. super().__init__(name, no_of_legs, sound_of_animal)
  5. def setOwner(self, owner):
  6. self.__owner = owner
  7. def printDetails(self):
  8. super().printDetails()
  9. print("Owner is", self.__owner)

Here we override the printDetails function called the base class printDetails function using super and increased it's functionality by adding another print statement to show the owner. Now let's add another class and see the real advantage of method overriding.

  1. class Lion(Animal):
  2. __habitatType = ""
  3. def __init__(self, name, no_of_legs,sound_of_animal):
  4. super().__init__(name, no_of_legs, sound_of_animal)
  5. def setHabitatType(self, habitatType):
  6. self.__habitatType = habitatType
  7. def printDetails(self):
  8. super().printDetails()
  9. print("Habitat Type", self.__habitatType)
  10. c = Cat("Kitty", 4, "Meow")
  11. c.setOwner("James")
  12. c.printDetails()
  13. l = Lion("Simba", 4, "Roar")
  14. l.setHabitatType("Forest")
  15. l.printDetails()

We added the Lion class and again we did method overriding of printDetails function to print habitatType instead of owner. So now we can note that instead of writing print statement again we can simply add the new print statements in the child class and can pick common print statements from the parent class function.

Private members of Parent Class

When the child class inherits from parent it inherits all its methods and properties except those that are private. So in order to restrict some methods and properties to be accessed by child class we need to restrict them and that can be done by making them private to that class. In Python a member can be made private by adding double underscore before the name. Note that in above example __owner is a private member in parent class

Example:

  1. class Vehicle:
  2. def __manual(self):
  3. print(“This is a private method”)

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.