41. Problem to create an inheritance example using Python

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

Inheritance example using Python

Steps to solve the program
  1. The Customer class is defined with a constructor method __init__ that takes name and balance as parameters and initializes the instance variables self.name and self.balance with the provided values.
  2. The class also has two methods, deposit and withdraw, to perform deposit and withdrawal operations on the customer’s account.
  3. The deposit method takes an amount as a parameter and adds it to the balance of the customer.
  4. The withdraw method takes an amount as a parameter and checks if the requested amount is less than or equal to the current balance. If so, it subtracts the amount from the balance. Otherwise, it prints “Insufficient balance”.
  5. The VIPCustomer class is defined as a subclass of Customer. It extends the functionality of Customer and adds additional attributes and methods specific to VIP customers.
  6. The __init__ method of VIPCustomer takes name, balance, credit_limit, and discount_rate as parameters. It calls the superclass Customer’s __init__ method using super() to initialize the name and balance attributes.
  7. Additionally, the VIPCustomer class introduces two new attributes, credit_limit and discount_rate, specific to VIP customers.
  8. The calculate_available_credit method calculates the available credit for the VIP customer by subtracting the current balance from the credit limit.
  9. The code creates an object vip_customer of the VIPCustomer class by calling its constructor and passing the name “John Doe”, an initial balance of 5000, a credit limit of 10000, and a discount rate of 0.1 as arguments.
  10. The calculate_available_credit method is called on the vip_customer object to determine the available credit.
  11. Finally, the available credit is printed using the print statement.
  12. Thus, we have created an inheritance example using Python.

Output:

Related Articles

Create a Python class called Customer with attributes name and balance.

Create a Python class called Phone with attributes brand, model, and storage.

Leave a Comment