32. Problem to create an inheritance example using Python

In this Python oops program, we will create an inheritance example using Python. Python class called CheckingAccount that inherits from the BankAccount class.

Inheritance example using Python

Steps to solve the program
  1. The BankAccount class has a constructor method __init__ that takes two parameters: account_number and balance. It initializes the instance variables self.account_number and self.balance with the provided values.
  2. The deposit method allows depositing money into the account. It takes an amount parameter, increases the account balance by that amount, and prints a success message along with the new balance.
  3. The withdraw method allows withdrawing money from the account. It takes an amount parameter and checks if the account balance is sufficient to cover the requested withdrawal amount. If so, it deducts the amount from the balance and prints a success message with the new balance. If the account balance is insufficient, it prints a message indicating the withdrawal is denied.
  4. The CheckingAccount class is a subclass of BankAccount and inherits its attributes and methods. It introduces two additional attributes: transaction_limit and transaction_fee. The constructor method __init__ of CheckingAccount calls the superclass’s __init__ method using super() to initialize the inherited attributes.
  5. The withdraw method is overridden in the CheckingAccount class to include additional validation specific to checking accounts. It calls the check_transaction_limit method to check if the withdrawal amount is within the transaction limit. If the withdrawal amount is valid and the account balance is sufficient to cover the withdrawal amount plus the transaction fee, it calls the superclass’s withdraw method to perform the withdrawal. If not, it prints a message indicating that the withdrawal is denied due to insufficient funds.
  6. The code creates an object checking_account of the CheckingAccount class with an account number of “1234567890”, an initial balance of 1000, a transaction limit of 500, and a transaction fee of 10.
  7. The withdraw method is called twice on the checking_account object. The first withdrawal is for an amount of 400, which is within the transaction limit and the available balance. The second withdrawal is for an amount of 600, which exceeds the transaction limit, resulting in a transaction fee being deducted. However, the account balance is insufficient to cover the withdrawal amount plus the transaction fee, so the withdrawal is denied.
  8. Thus, we have created an inheritance example using Python.

Output:

Related Articles

Create a Python class called SavingsAccount that inherits from the BankAccount class.

Create a Python class called Car with attributes make, model, and year.

Leave a Comment