Java Interface
2026-03-28 00:43
Tags: #java
Author: Duke Hsu
Key Concept
- interface is a template that can be applied to a class.
- interface similar to inheritance, but specifies what a class has/ must do.
- classes can apply more than one interface,

Code Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 | /**
*
*/
package bankingsystem;
/**
*
*/
public class Bank {
/**
* @param args
*/
public static void main(String[] args) {
//initd object
SavingAccount dkAccount = new SavingAccount("duke", 5000);
// call methods - interface BankAccount and InteresEarning
dkAccount.deposit(9000);
dkAccount.deposit(100000);
dkAccount.withdraw(5000);
System.out.println("**********************************");
dkAccount.printStatement();
System.out.println();
System.out.println("==================================");
dkAccount.applyInterest();
System.out.println("InterestRate is: "+dkAccount.getInterestRate());
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 | package bankingsystem;
//class SavingAccount can apply more than one interface - BankAccount,InteresEarning
public class SavingAccount implements BankAccount,InteresEarning{
private double balance;
private String accountHoulder;
private double interestRate =0.03;
//constructor
public SavingAccount(String name, double initialBanlance) {
this.accountHoulder = name;
this.balance = initialBanlance;
}
@Override
public void deposit(double amount) {
balance += amount;
System.out.println("Account: " + accountHoulder + " Deposited: " + amount);
}
@Override
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Account: " +accountHoulder + " Withdraw: " + amount);
}else {
System.out.println("Isufficient funds!!!");
}
}
@Override
public double getBalance() {
return balance;
}
@Override
public void printStatement() {
System.out.println("=== Savings Account ===");
System.out.println("Holder: " + accountHoulder);
System.out.println("Balance: $" + balance);
}
@Override
public void applyInterest() {
double interest = balance * interestRate;
balance += interest;
System.out.println(" Interest applied: $" + interest);
System.out.println(" New balance: $" + balance);
}
@Override
public double getInterestRate() {
return interestRate;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 | package bankingsystem;
public class CheckingAccount implements BankAccount {
private double balance;
private String accountHolder;
public CheckingAccount(String name, double initialBalance) {
this.accountHolder = name;
this.balance = initialBalance;
}
@Override
public void deposit(double amount) {
balance += amount;
System.out.println("💰 " + accountHolder + " deposited $" + amount);
}
@Override
public void withdraw(double amount) {
// overdraft
balance -= amount;
System.out.println("💸 " + accountHolder + " withdrew $" + amount);
}
@Override
public double getBalance() {
return balance;
}
@Override
public void printStatement() {
System.out.println("=== Checking Account ===");
System.out.println("Holder: " + accountHolder);
System.out.println("Balance: $" + balance);
}
}
|
| package bankingsystem;
//this interface can implements to class
public interface BankAccount {
void deposit(double amount); //deposit money
void withdraw(double amount); // withdraw money
double getBalance(); //balance check
void printStatement(); //print statements
}
|
- Interface InterestEarning
| package bankingsystem;
// this interface can implements to class
public interface InteresEarning {
void applyInterest();
double getInterestRate();
}
|
Note
Use Command + Shift + V paste the code from Eclipse
References