Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Method overriding in Python language

Posted on January 29, 2018January 10, 2020

Table of Contents

  • Method overriding in Python language
    • Explanation of method overriding
    • Example for overriding

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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes