3
Reply

Interface

Shilpesh Sinha

Shilpesh Sinha

Aug 20 2015 2:23 AM
390
Consider a Banking Scenario, There are many accounts, like Savings Account, Current Account, Demat Account and so on. We have a base Class Account which contains all the basic properties and methods of an Account. There are some Maintainance Charges that applies to only some of the accounts. If you would like to enforce maintainance charges to the the Savings Account & Current Account, then the simplest way is for the class to implement the interface.

Therefore, programming Interfaces essentially acts like a contract, where it is given that the methods declared in the interface has to be implemented in the class. Let us code the above Scenario.

The base class will be Account. In that create the fields as name, number, balance and startDate.
Create an interface MaintenanceCharge.

Write the below method in the interface
float calculateMaintancecharge(float noofyears)

The CurrentAccount & SavingsAccount which extends Account and implements MaintainanceCharge interface.
In Savings Account the maintenance amount will be 2mn+50.
In Current Account, the maintenance amount will be mn+200.
where m is the maintenance charge per year and n is the number of years.

Set the values via constructor.

Note: Maintenance charge is Rs.50 for a saving account and Rs.100 for a Current account.

Sample input and output:
1. Current Account
2. Savings Account
1
Name
Current
Account Number
12345
Account Balance
5000
Enter the No. of Years
2
Account Name: Current
Account Number: 12345
Account Balance: 5000
Maintenance Charge For Current Account is Rs. 400

Sample input and output:
1.Current Account
2.Savings Account
2
Name
SB
Account Number
54321
Account Balance
3000
Enter the No. of Years
5
Account Name: SB
Account Number: 54321
Account Balance: 3000
Maintenance Charge For Savings Account is Rs. 550

Answers (3)