forked from anirudhagaikwad/JavaFullStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP15_Interface.java
More file actions
183 lines (148 loc) · 5.7 KB
/
Copy pathP15_Interface.java
File metadata and controls
183 lines (148 loc) · 5.7 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package oop;
/*
An interface in Java is a blueprint of a class that defines abstract methods.
It is declared using the interface keyword and is used to achieve 100% abstraction since all methods
in an interface are abstract by default (except default and static methods).
### Characteristics of an Interface:
- Declared using interface keyword.
- Cannot have instance variables (only static and final constants allowed).
- Supports multiple inheritance: A class can implement multiple interfaces.
- All methods are public and abstract by default, except default and static methods.
- Cannot have constructors since an interface cannot be instantiated.
- A class implementing an interface must provide implementations for all its methods.
### Types of Methods in an Interface:
- Abstract Methods - Methods without a body (implicitly public and abstract).
- Default Methods - Methods with a body using the default keyword (introduced in Java 8).
- Static Methods - Methods with a body using the static keyword (introduced in Java 8).
###
Interfaces no longer provide 100% abstraction after Java 8. However, they still enforce a strict contract
for implementing classes, making them useful for abstraction and multiple inheritance.
*/
/*
Banking System with Interface Multiple Inheritance
Design a banking system where different bank services (Account Management, Transactions, and Interest Calculation) are handled using interfaces. Implement multiple inheritance using interfaces and demonstrate:
Defining an interface with constants and methods.
Using default and static methods.
Implementing multiple interfaces in a single class.
*/
public class P15_Interface {
public static void main(String[] args) {
BankAccount_ account = new BankAccount_("Anirudha", 1000);
// Default method from interface
account.showBankRules();
// Static methods from interfaces
Banking.bankingGuidelines();
Transactions.transactionPolicy();
// Deposit and Withdraw transactions
account.deposit(500);
account.withdraw(200);
account.withdraw(2000); // Invalid Withdrawal
// Interest Calculation
double interest = account.calculateInterest(1300, 5);
System.out.println("Interest on balance: " + interest);
// Enable Card Security Feature
account.enableTwoFactorAuthentication();
// Close the account
account.closeAccount();
}
}
//Main Banking Interface
interface Banking {
// 1. Constant Field (public static final by default)
double MIN_BALANCE = 500; // Minimum required balance
// 2. Abstract Methods (Must be implemented by classes)
void openAccount(String name, double initialDeposit);
void closeAccount();
// 3. Default Method (Implemented inside interface)
default void showBankRules() {
System.out.println("Bank Rules: Minimum balance should be " + MIN_BALANCE);
}
// 4. Static Method (Belongs to interface, called directly)
static void bankingGuidelines() {
System.out.println("Banking Guidelines: Secure transactions, regular account updates.");
}
}
//Transactions Interface (Extends Banking)
interface Transactions extends Banking {
void deposit(double amount);
void withdraw(double amount);
// 5. Private Method (Used inside the interface)
private boolean isValidAmount(double amount) {
return amount > 0;
}
// 3. Default Method using Private Method
default boolean isTransactionValid(double amount) {
return isValidAmount(amount);
}
// 4. Static Method
static void transactionPolicy() {
System.out.println("Transactions above 10,000 require OTP verification.");
}
}
//Interest Interface (Separate functionality)
interface Interest {
double calculateInterest(double balance, double rate);
}
//Nested Interface Example
interface CardServices {
void activateCard();
void deactivateCard();
// 7. Nested Interface
interface CardSecurity {
void enableTwoFactorAuthentication();
}
}
//Class implementing multiple interfaces
class BankAccount_ implements Transactions, Interest, CardServices.CardSecurity {
private String accountHolder;
private double balance;
// Constructor
public BankAccount_(String name, double initialDeposit) {
openAccount(name, initialDeposit);
}
// Implementing Banking (From Transactions)
@Override
public void openAccount(String name, double initialDeposit) {
this.accountHolder = name;
if (initialDeposit >= MIN_BALANCE) {
this.balance = initialDeposit;
System.out.println("Account opened for " + name + " with balance: " + initialDeposit);
} else {
System.out.println("Initial deposit must be at least " + MIN_BALANCE);
}
}
@Override
public void closeAccount() {
System.out.println("Account closed for " + accountHolder);
this.balance = 0;
}
// Implementing Transactions
@Override
public void deposit(double amount) {
if (isTransactionValid(amount)) {
balance += amount;
System.out.println("Deposited: " + amount + " | New Balance: " + balance);
} else {
System.out.println("Invalid deposit amount!");
}
}
@Override
public void withdraw(double amount) {
if (isTransactionValid(amount) && balance - amount >= MIN_BALANCE) {
balance -= amount;
System.out.println("Withdrawn: " + amount + " | Remaining Balance: " + balance);
} else {
System.out.println("Invalid withdrawal! Minimum balance required: " + MIN_BALANCE);
}
}
// Implementing Interest
@Override
public double calculateInterest(double balance, double rate) {
return balance * rate / 100;
}
// Implementing Nested Interface Method (Card Security)
@Override
public void enableTwoFactorAuthentication() {
System.out.println("Two-factor authentication enabled for account security.");
}
}