python tutorial - Inheritance in Python - learn python - python programming




 python inheritance

What is Inheritance?

  • Inheritance is used to specify that one class will get most or all of its features from its parent class. It is a feature of Object Oriented Programming.
  • It is a very powerful feature which facilitates users to create a new class with a few or more modification to an existing class.
  • The new class is called child class or derived class and the main class from which it inherits the properties is called base class or parent class.
  • The child class or derived class inherits the features from the parent class, adding new features to it. It facilitates re-usability of code.

Python Inheritance Syntax:

class BaseClass:
  Body of base class
class DerivedClass(BaseClass):
  Body of derived class
  • Derived class inherits features from the base class, adding new features to it. This results into re-usability of code.

Example of Inheritance in Python:

  • To demonstrate the use of inheritance, let us take an example.
  • A polygon is a closed figure with 3 or more sides. Say, we have a class called Polygondefined as follows.
class Polygon:
    def __init__(self, no_of_sides):
        self.n = no_of_sides
        self.sides = [0 for i in range(no_of_sides)]

    def inputSides(self):
        self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in range(self.n)]

    def dispSides(self):
        for i in range(self.n):
            print("Side",i+1,"is",self.sides[i])
  • This class has data attributes to store the number of sides, n and magnitude of each side as a list, sides.
  • Method inputSides() takes in magnitude of each side and similarly, dispSides() will display these properly.
  • A triangle is a polygon with 3 sides. So, we can created a class called Triangle which inherits from Polygon.
  • This makes all the attributes available in class Polygon readily available in Triangle. We don't need to define them again (code re-usability). Triangle is defined as follows.
class Triangle(Polygon):
    def __init__(self):
        Polygon.__init__(self,3)

    def findArea(self):
        a, b, c = self.sides
        # calculate the semi-perimeter
        s = (a + b + c) / 2
        area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
        print('The area of the triangle is %0.2f' %area)
  • However, class Triangle has a new method findArea() to find and print the area of the triangle.
  • Here is a sample run.
>>> t = Triangle()

>>> t.inputSides()
Enter side 1 : 3
Enter side 2 : 5
Enter side 3 : 4

>>> t.dispSides()
Side 1 is 3.0
Side 2 is 5.0
Side 3 is 4.0

>>> t.findArea()
The area of the triangle is 6.00
  • We can see that, even though we did not define methods like inputSides() or dispSides()for class Triangle, we were able to use them.
  • If an attribute is not found in the class, search continues to the base class. This repeats recursively, if the base class is itself derived from other classes.

Image representation:

 python class inheritance
 derived class in python

Syntax 1:

	class DerivedClassName(BaseClassName):  
	    <statement-1>  
	    .  
	    .  
	    .  
	    <statement-N>  

Syntax 2:

	class DerivedClassName(modulename.BaseClassName):  
	    <statement-1>  
	    .  
	    .  
	    .  
    <statement-N>

Parameter explanation:

  • The name BaseClassName must be defined in a scope containing the derived class definition.
  • You can also use other arbitrary expressions in place of a base class name.
  • This is used when the base class is defined in another module.

Python Inheritance Example

  • Let's see a simple python inheritance example where we are using two classes: Animal and Dog. Animal is the parent or base class and Dog is the child class.
  • Here, we are defining eat() method in Animal class and bark() method in Dog class.
  • In this example, we are creating instance of Dog class and calling eat() and bark() methods by the instance of child class only.
  • Since, parent properties and behaviors are inherited to child object automatically, we can call parent and child class methods by the child instance only.
class Animal:
def eat(self):
print 'Eating...'
class Dog(Animal):
def bark(self):
print 'Barking...'
d=Dog()
d.eat()
d.bark()

Python Inheritance Example Output:

Eating...  
Barking...  

Related Searches to Inheritance in Python

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×