[Tutor] Method overriding

D-Man dsh8290@rit.edu
Thu, 15 Mar 2001 11:05:51 -0500


On Thu, Mar 15, 2001 at 07:55:41AM +0200, jovoslav@dir.bg wrote:
| Hello,
| Can someone please define what method overriding means.

Here is an example :

class P :
    def foo( self ) :
        print "Hello"
    def bar( self ) :
        print "Bar"

class C( P ) :
    def foo( self ) :
        print "GoodBye"

obj1 = P()
obj2 = C()

# an instance of class 'C' is-a instance of class 'P' because of the
# inheritance,
# however it will be have differently.

obj1.foo()  # prints "Hello"
obj2.foo()  # prints "GoodBye"


Class 'C' overrode the definition of method foo in class 'P' to make
instances of class 'C' behave differently.  It didn't override 'bar'
so:


obj1.bar()  # prints "Bar"
obj2.bar()  # prints "Bar"


-D