class TestAccountInterface
{
public static void main(String s[])
{
IAccount account = new HDFCAccount();
System.out.println("Transacting using HDFC Account");
transactOnAccount(account);
System.out.println();
account = new StateBankAccount();
System.out.println("Transacting using State Bank Account");
transactOnAccount(account);
}
public static void transactOnAccount(IAccount account)
{
System.out.println("------------------------------");
account.deposit(10000.0);
printBalance("depositing 10,000.0", account);
account.withdraw(2500.0);
printBalance("withdrawing 2,500.0", account);
account.withdraw(4100.0);
printBalance("withdrawing 4,100.0", account);
account.deposit(5000.0);
printBalance("depositing 5,000.0", account);
System.out.println("------------------------------");
}
public static void printBalance(String message, IAccount account)
{
System.out.println("The balance after " + message + " is " + account.getBalance() +".");
}
}
interface IAccount
{
double getBalance();
void deposit(double amount);
void withdraw(double amount);
}
class HDFCAccount implements IAccount
{
double deposits;
double withdrawals;
public double getBalance()
{
return deposits - withdrawals;
}
public void deposit(double amount)
{
deposits += amount;
}
public void withdraw(double amount)
{
withdrawals += amount;
}
}
class StateBankAccount implements IAccount
{
double balance;
public double getBalance()
{
return balance;
}
public void deposit(double amount)
{
balance += amount;
}
public void withdraw(double amount)
{
balance -= amount;
}
}
Transacting using HDFC Account
------------------------------
The balance after depositing 10,000.0 is 10000.0.
The balance after withdrawing 2,500.0 is 7500.0.
The balance after withdrawing 4,100.0 is 3400.0.
The balance after depositing 5,000.0 is 8400.0.
------------------------------
Transacting using State Bank Account
------------------------------
The balance after depositing 10,000.0 is 10000.0.
The balance after withdrawing 2,500.0 is 7500.0.
The balance after withdrawing 4,100.0 is 3400.0.
The balance after depositing 5,000.0 is 8400.0.
------------------------------
Here we have created an interface IAccount
and declared the methods getBalance
, deposit
and withdraw
. This interface is implemented by two classes HDFCAccount
and StateBankAccount
. If we carefully observe, there is difference between implementation of these classes, HDFCAccount
uses member variables deposits
and withdrawals
for maintaining the balance, where as StateBankAccount
uses only balance
to maintain the balance.
In the main
method we have created objects of HDFCAccount
and StateBankAccount
, but assigned them to the reference of the interface IAccount
. The methods transactOnAccount
and printBalance
only know about the interface IAccount
and does not getting impacted on which class's object is passed.
ICICIAccount
which implements the interface IAccount
and implement the required methods. In the main
method create an object of ICICIAccount
and see if you get the same output as the other class objects.