In this article we want to learn about Method Overriding in Python, so method overriding
is used for changing the implementation of a method provided by one of it is parent or
base class.
OK let’s create our practical example on Method Overriding in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Person: def full_name(self): print("My name is Geekscoders") class Student(Person): def full_name(self): print("My name is Parwiz") p = Person() p.full_name() st = Student() st.full_name() |
So in the above example we have two classes, the first class is our Person Class and
it is our base class, also we have added a method of def full_name() , on the other hand
we have another class at name of Student class and this class is extending from the base class,
so for this we can say that it is a derived class, now you can see that we have the same name
method in our derived class, when you have two methods with the same name and different
implementation that is called Method Overriding, and these two methods have their own
implementation, even tough they have the same name but the implementations are different.
- How to Create Class in Python
- Python Class Variables VS Instance Variables
- Python Class Inheritance
- Super() Function in Python
If you run the code you can see that we have different implementation for the
same method name.
