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.- class Animal:
- name = ""
- no_of_legs = 0
- sound_of_animal = ""
- def __init__(self, name, no_of_legs, sound_of_animal):
- self.name = name
- self.no_of_legs = no_of_legs
- self.sound_of_animal = sound_of_animal
- class Cat(Animal):
- __owner = ""
- def __init__(self, name, no_of_legs,sound_of_animal):
- super().__init__(name, no_of_legs, sound_of_animal)
- def setOwner(self, owner):
- self.__owner = owner
- def printDetails(self):
- print("Owner is", self.__owner)
- print("Cat name", self.name)
- print("No of Legs", self.no_of_legs)
- print("Sound of Cat", self.sound_of_animal)
Overriding in Python OOP
Example:
- class Animal:
- name = ""
- no_of_legs = 0
- sound_of_animal = ""
- def __init__(self, name, no_of_legs, sound_of_animal):
- self.name = name
- self.no_of_legs = no_of_legs
- self.sound_of_animal = sound_of_animal
- def printDetails(self):
- print("\n\nAnimal Name", self.name)
- print("No of Legs", self.no_of_legs)
- print("Sound of Animal", self.sound_of_animal)
- class Cat(Animal):
- __owner = ""
- def __init__(self, name, no_of_legs,sound_of_animal):
- super().__init__(name, no_of_legs, sound_of_animal)
- def setOwner(self, owner):
- self.__owner = owner
- def printDetails(self):
- super().printDetails()
- print("Owner is", self.__owner)
- class Lion(Animal):
- __habitatType = ""
- def __init__(self, name, no_of_legs,sound_of_animal):
- super().__init__(name, no_of_legs, sound_of_animal)
- def setHabitatType(self, habitatType):
- self.__habitatType = habitatType
- def printDetails(self):
- super().printDetails()
- print("Habitat Type", self.__habitatType)
- c = Cat("Kitty", 4, "Meow")
- c.setOwner("James")
- c.printDetails()
- l = Lion("Simba", 4, "Roar")
- l.setHabitatType("Forest")
- l.printDetails()
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:
- class Vehicle:
- def __manual(self):
- print(“This is a private method”)
Add new comment
- 222 views