Interfaces can be used for future task implementation of the functionality.
Account Interface
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;
}
}
OUTPUTTransacting 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.
------------------------------
DESCRIPTIONHere 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.
THINGS TO TRY
- Create one more class
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.
As shown in the above program we could have implemented methods
transactOnAccount
and
printBalance
and most of the
main
method with out the knowledge of the concrete classes like
HDFCAccount
,
StateBankAccount
. These methods can be implemented only using the interface
IAccount
.
This is an example of deferring the implementation, but coding to the interface. This offers a great advantage while developing large applications. Usually large applications contain lot of small modules and a different person/team develops a separate module. Since there will be dependencies between modules, it would not a good idea to wait until all the required modules are ready. Instead we usually define only the interfaces between modules and develop using those interfaces. When the implementation of that dependent module is ready, we might make a very few minor changes and start testing that implementation of that module.