Chapter 12 Classes

12.9 Overriding MethodsΒΆ

Having inherited all the Account methods, the next step to take for the definition of CreditAccount is to define the parts that are different from the base Account class. There should be two main changes. First, the constructor needs to be modified, adding the assignment of another attribute: the credit limit. Second, the withdraw method needs to be modified, so that overdraft is allowed within the credit limit.

To apply the changes above, the constructor and the withdraw method need to be redefined in the new class, overriding those in the base class. Save the following code as account2.py at c:\myPython folder.

class Account:
    total =100001
    def __init__(self, name, balance=0):
        self.name=name
        self.balance=balance
        self.number=Account.total
        Account.total+=1

    def deposit(self , amount):
        if amount <=0:
            print ('Error , negative amount.')
            return
        self.balance+=amount

    def withdraw(self, amount):
        if amount <0:
            print ('Error: negative amount')
            return
        if amount >self.balance:
            print ('Error: insufficient fund')
            return
        self.balance -=amount

    def __str__(self):
        resultStr='Account holder name: ' + self.name + ', Account number: ' + \
        str(self.number) + ', Balance: ' + str(self.balance) + '.'
        return resultStr

class CreditAccount(Account):
   def __init__(self, name, balance=0, limit=0):
       Account.__init__(self, name, balance)
       self.limit=limit

   def withdraw(self, amount):
       if amount <0:
           print ('Error: negative amount.')
           return

       if amount >self.balance+self.limit:
           print ('Error: insufficient fund')
           return
       self.balance -=amount

At c:\myPython folder, starts IDLE (or python) and do the followings:

>>> from account2 import *
>>> a1=Account('John')
>>> a2=CreditAccount('Marina', limit=500)
>>> a1.balance
0
>>> a2.balance
0
>>> a1.withdraw(100)
Error: insufficient fund
>>> a2.withdraw(100)
>>> a1.balance
0
>>> a2.balance
-100

In the example above, CreditAccount is redefined, with its own constructor and withdraw method. Several instances of Account and CreditAccount are constructed, which now behave diffferently. The CreditAccount instance, a2 is constructed by specifying a credit limit, limit=500. Initially, the balances of both a2 and the Account instance a1 are 0s. When a1.withdraw(100) is called, an error is reported and the transaction is cancelled. However, when a2.withdraw(100) is called, the transaction proceeds even though the balance is smaller than the amount withdrawn, leaving a negative balance in a2. The different behavior results in the overriding of the withdraw methods.

The new CreditAccount instances have a new attributes, limit, which specifies the credit limit of the account. The constructor of CreditAccount takes an additional argument that specifies the credit limit, and assigns it to the limit attribute of the new instance to be constructed. The rest of the construction, including the assignment of name, number and balance, is the same as that for Account. As a result, the constructor of CreditAccount simply calls the base constructor Account.__init__, with the new CreditAccount instance self being passed as the self argument, and the input name and balance arguments being passed as the name and balance arguments for Account.__init__, respectively. Execution of Account.__init__ in turn assigns the respective attributes to self the new CreditAccount instance, and increases Account.total by one.

The withdraw method of CreditAccount is different from that of Account in that the prerequisite is amount<balance+limit now instead of amount<balance. Because the new prerequisite is less strict than the prerequisite in the base withdraw method, the new withdraw method cannot call the base method in the same way as the __init__ method does, but has to rewrite the complete procedure. Nevertheless, by inheriting from the base class, the CreditAccount class is significantly simplified as compared to an implementation written from scratch.

© Copyright 2024 GS Ng.

Next Section - 12.10 Object Oriented Programming