0% found this document useful (0 votes)
20 views7 pages

Java Interface Example: Bank Interest Calculation

The document provides an example of a Java interface named 'Bank' for calculating interest, which is implemented by the class 'SBI' using a simple interest formula. It demonstrates how to use the interface in a main class to calculate and display interest from SBI and also includes an optional extension with 'HDFC' that adds a fixed amount to the interest. The example illustrates the concept of polymorphism in Java through interface implementation.

Uploaded by

mohanapriya.c
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

Java Interface Example: Bank Interest Calculation

The document provides an example of a Java interface named 'Bank' for calculating interest, which is implemented by the class 'SBI' using a simple interest formula. It demonstrates how to use the interface in a main class to calculate and display interest from SBI and also includes an optional extension with 'HDFC' that adds a fixed amount to the interest. The example illustrates the concept of polymorphism in Java through interface implementation.

Uploaded by

mohanapriya.c
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

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));

You might also like