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
    • Related

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

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes