Table of Contents
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)
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.
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.