31. Problem to create an inheritance example in real life.

In this Python oops program, we will create an inheritance example in real life. In this program, a class called SavingsAccount inherits from the BankAccount class.

Inheritance example in real life

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 SavingsAccount class is a subclass of BankAccount and inherits its attributes and methods. It introduces two additional attributes: interest_rate and minimum_balance. The constructor method __init__ of SavingsAccount calls the superclass’s __init__ method using super() to initialize the inherited attributes.
  5. The calculate_interest method calculates and prints the interest earned on the account balance based on the provided interest_rate.
  6. The withdraw method is overridden in the SavingsAccount class to include additional validation specific to savings accounts. It checks if the withdrawal amount allows the account to maintain the minimum required balance. If the withdrawal is within the limits, 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 not meeting the minimum balance requirement.
  7. An object of the SavingsAccount class is created with an account number of “1234567890”, an initial balance of 1000, an interest rate of 2.5%, and a minimum balance requirement of 500. The savings_account variable refers to this object.
  8. The calculate_interest method is called on the savings_account object, which calculates and prints the interest earned based on the account balance and interest rate.
  9. The withdraw method is called on the savings_account object with an amount of 800. It checks if the withdrawal amount maintains the minimum required balance and calls the superclass’s withdraw method if valid. In this case, the withdrawal is allowed, so the superclass’s withdraw method is called and a success message is printed with the new balance.
  10. Thus, we have created an Inheritance example in real life.

Output:

Related Articles

Create a Python class called BankAccount with attributes account_number and balance.

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

Leave a Comment