Java Interface Example – Bank
Interest Calculation
Simple Implementation using
Interface
1. Interface Definition
interface Bank {
double calculateInterest(double principal,
double rate, int time);
}
- Defines the contract for interest calculation.
- Accepts principal, rate, and time.
2. Implementation Class: SBI
class SBI implements Bank {
public double calculateInterest(double principal,
double rate, int time) {
return (principal * rate * time) / 100;
}
}
• - Implements simple interest formula: (P × R ×
T) / 100
3. Main Class to Test It
public class BankExample {
public static void main(String[] args) {
Bank sbi = new SBI();
double interest = [Link](10000, 5,
2);
[Link]("Interest from SBI: ₹" +
interest);
}
}
4. Output
• Interest from SBI: ₹1000.0
5. Explanation
- 'Bank' is the interface with method declaration.
- 'SBI' provides actual implementation.
- Main method shows how interface enables
polymorphism.
- Interface helps define rules without worrying
about implementation.
6. Optional Extension: HDFC Bank
class HDFC implements Bank {
public double calculateInterest(double principal, double
rate, int time) {
return (principal * rate * time) / 100 + 50;
}
}
Bank hdfc = new HDFC();
[Link]("Interest from HDFC: ₹" +
[Link](10000, 5, 2));