How to override methods in Python
Override methods
Overriding is an important aspect of the inheritance mechanism in general. It occurs when a method from the base class has the same signature with the method from the child class.
class A: # from w ww .jav a 2s.c o m
def hello(self):
print "Hello, I'm A."
class B(A):
def hello(self):
print "Hello, I'm B."
a = A()
b = B()
a.hello()
b.hello()
The code above generates the following result.
In the code above we create class B
by inheriting from class
A
. In class A
there is a method called hello. In class B
we override hello method and change
to display new message.