Interface in Java

Interface in Java defines a IS-A relationship between classes and a mechanism to achieve abstraction. An Interface only consist of method definition and not the method body.

 

Benefits of Using Abstraction

  1. Provides a contract for a Class that  implements the interface. That is to provide a common behaviour for multiple classes that implements the interface.
  2. The Class that implements Interface should implement all the methods defined in the Interface.
  3. It can be used to achieve loose coupling
  4. It can be used to support multiple inheritance in Java.

 

Interface Example

public interface IBankInterface {

    void openAccount();

    void showBalance();

    void withdrawAmount();

    void depositAmount();

    void changePhoneNumber();

    void closeAccount();
}

The above code shows an Interface definition for a BankAccount which defines the general method of operations of a Bank Account. This Interface can be implemented by different Types of Bank Account (Savings, Current, Checking) and this will make sure that all the type of Bank Account Must implement the defined methods in the Interface.

Class Implementing Interface.

public class SavingsAccount implements IBankInterface {

    @Override
    public void openAccount() {
        System.out.println("Account is Opened");

    }

    @Override
    public void showBalance() {
        System.out.println("Showing Current Balance");

    }

    @Override
    public void withdrawAmount(double amount) {
        System.out.println("Withdrawing Amount " + amount);
    }

    @Override
    public void depositAmount(double amount) {
        System.out.println("Depositing Amount " + amount);
    }

    @Override
    public void changePhoneNumber(String phoneNumber) {
        System.out.println("Phone Number Changed to  " + phoneNumber);
    }

    @Override
    public void closeAccount() {
        System.out.println("Account Closed");
    }

}

Consider the example of the Class SavingsAccount that implements the Interface IBankInterface has to implement all the methods defined by the Interface.

Let’s Consider a Main Class from where we will try to Run this code.

public class Main {

    public static void main(String args[]) {
        IBankInterface account = new SavingsAccount();
        account.openAccount();
        account.depositAmount(500);
        account.withdrawAmount(200);
        account.changePhoneNumber("232-232-2323");
        account.closeAccount();

    }

}

This code will produce the following output.

Account is Opened
Depositing Amount 500.0
Withdrawing Amount 200.0
Phone Number Changed to 232-232-2323
Account Closed

If you look into the code we have Initialised the Class SavingsAccount and saved its Object in type IBankInterface and thus later we can replace this with any other type of Account CurrentAccount or CheckingAccount and it will work perfectly fine. Thus while passing Argument its always better to consider passing Interface to a Method definition and then we can use any Class implementation to pass into the method.

tgugnani: Web Stuff Enthusiast.