Categories: Oop pythonPython

Method overriding in Python language

Method overriding in Python language

In this tutorial, we will discuss method overriding in Python language.

Method overriding is a very important concept in object-oriented programming languages.

It allows to change the implementation of the function that is already defined in the parent class.

This means having two methods with the same name but performing different tasks. One of the methods would override the other method(Child class overrides parent class)

Explanation of method overriding

We will try to understand what method overriding is using this python explanation.

This program has two classes, named A and B. Class A is the parent class and class B is the child class. Both classes have the same name.

When we call the method, method inside the child class overrides the method inside the parent class.

So the child class statement inside the method body is executed.

Example for overriding

Program 1

This is an example of inheritance in Python.

Output

This method is inside the base class.

Program 2

Overriding is the ability of a class to change the implementation of the method provided by one of its ancestors.
This program is similar to program 1 but it has a method inside the derived class with the same name but different method-body.

In this program, method belonging to derived class overrides the methods belonging to the base class.

Output

This method is inside the class derived.

Program 3

Output

This is the son’s name
This is the last name

In this program, there are two classes. One of them is the father class(superclass) and the other class is the son(subclass). Method to print_firstname is available in both classes. When we call the method, the subclass (class son) method override superclass (class father) method.

Program 4

In this program, there are two classes. One of them is the class Vehicle(superclass) and another class is Car(subclass).

The getvalue() method is available in both classes. Getvalue method inside the subclass (class Car)  overrides other getvalue() method is inside the superclass (class Vehicle).

Output

Child constructor

method in child class
second method in child class

Similar post

Abstract class in C++                                           Abstract class in Java

Encapsulation in C++                                       Encapsulation in Java

Polymorphism in C++                                      Methodoverriding in Java

Method overloading in Java                             Constructor overloading in Java

Exception Handling in Java

Karmehavannan

Share
Published by
Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

4 weeks ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

4 weeks ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.