Python Method Overriding
In this tutorial you will learn, how method overriding works in Python.
One prominent feature of many object-oriented programming languages is a tool called method overriding, where two objects can have identically named methods that can work differently from one another.
To use method overriding, you simply create a new class that inherits from an existing class. In this new class, you create a method that's identically named as a method in the other class.
Method Overriding
2019-01-12T22:31:03+05:30
2019-01-12T22:31:03+05:30
Amit Arora
Amit Arora
Python Programming Language Tutorial
Python Tutorial
Programming Tutorial
Example of Method Overriding
Sometimes the class provides a generic method, but in the child class, the user wants a specific implementation of the method.
Example
Classes Triangle, Rhombus, Pentagon and Hexagon have the same methods area() and perimeter() with different implementations:
import math # Super class class Polygons: def number_of_sides(self): return 0 def area(self): return 0 def perimeter(self): return 0 # Triangle class inherits from Polygons class Triangle(Polygons): def number_of_sides(self): return 3 def area(self, base, height): return 1 / 2 * base * height def perimeter(self, a, b, c): if a + b > c: return a + b + c else: return "Invalid input: make sure a + b > c" # Rhombus class inherits from Polygons class Rhombus(Polygons): def number_of_sides(self): return 4 def area(self, p, q): return p * q / 2 def perimeter(self, a): return 4 * a # Pentagon class inherits from Polygons class Pentagon(Polygons): def number_of_sides(self): return 5 def area(self, a): return 1 / 4 * math.sqrt(5 * (5 + 2 * math.sqrt(5))) * a ** 2 def perimeter(self, a): return 5 * a # Hexagon class inherits from Polygons class Hexagon(Polygons): def number_of_sides(self): return 6 def area(self, a): return (3 * math.sqrt(3) / 2) * a ** 2 def perimeter(self, a): return 6 * a # Below is some test cases. tri = Triangle() print("Triangle Area:", tri.area(15, 25)) print("Perimeter:", tri.perimeter(15, 20, 25)) print("-----------------") rho = Rhombus() print("Rhombus Area:", rho.area(15, 25)) print("Perimeter:", rho.perimeter(30)) print("-----------------") pent = Pentagon() print("Pentagon Area:", pent.area(15)) print("Perimeter:", pent.perimeter(25)) print("-----------------") hex = Hexagon() print("Hexagon Area:", hex.area(15)) print("Perimeter:", hex.perimeter(25)) print("-----------------")
Output
Triangle Area: 187.5 Perimeter: 60 ----------------- Rhombus Area: 187.5 Perimeter: 120 ----------------- Pentagon Area: 387.10741513251753 Perimeter: 125 ----------------- Hexagon Area: 584.5671475544962 Perimeter: 150 -----------------