-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIS_A_DemoP3.java
More file actions
69 lines (56 loc) · 1.66 KB
/
IS_A_DemoP3.java
File metadata and controls
69 lines (56 loc) · 1.66 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
class Account_1 {
String name;
int acc_no = 121212;
void deposit() {
System.out.println("Deposit Limit is 100000");
}
void withdraw() {
System.out.println("Withdraw Limit is 50000");
}
}
class CurrentAccount_1 extends Account_1 {
void minBalance() {
System.out.println("Min balance must be 5000");
}
// @Override - annotation - just to tell other developer that
// we are overriding this method
@Override
void withdraw() {
System.out.println("Withdraw Limit is 35000");
}
}
class SavingAccount_1 extends Account_1 {
double balance;
void roi() {
System.out.println("ROI is 6%");
}
@Override
void deposit() {
System.out.println("Deposit Limit is 50000");
}
}
public class IS_A_DemoP3 {
// Polymorphic call
void caller(Account_1 acc) {
acc.deposit();
acc.withdraw();
// DownCasting
if(acc instanceof SavingAccount_1) {
//((SavingAccount_1) acc).roi();
SavingAccount_1 sa = (SavingAccount_1) acc;
sa.roi();
}
else if(acc instanceof CurrentAccount_1) {
//((CurrentAccount_1) acc).minBalance();
CurrentAccount_1 ca = (CurrentAccount_1) acc;
ca.minBalance();
}
}
public static void main(String[] args) {
IS_A_DemoP3 obj = new IS_A_DemoP3();
// Account acc = new SavingAccount();
// UpCasting
obj.caller(new CurrentAccount_1());
obj.caller(new SavingAccount_1());
}
}