-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIS_A_DemoP2.java
More file actions
75 lines (59 loc) · 1.95 KB
/
IS_A_DemoP2.java
File metadata and controls
75 lines (59 loc) · 1.95 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
class Account {
String name;
int acc_no;
void deposit() {
System.out.println("Deposit Limit is 100000");
}
void withdraw() {
System.out.println("Withdraw Limit is 50000");
}
}
class CurrentAccount extends Account {
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 extends Account {
double balance;
void roi() {
System.out.println("ROI is 6%");
}
@Override
void deposit() {
System.out.println("Deposit Limit is 50000");
}
}
//DRY - Donot Repeat Yourself
public class IS_A_DemoP2 {
public static void main(String[] args) {
// SavingAccount sa = new SavingAccount();
// sa.deposit();
// sa.withdraw();
// sa.roi();
//
// CurrentAccount ca = new CurrentAccount();
// ca.deposit();
// ca.withdraw();
// ca.minBalance();
// Object obj = new SavingAccount();
// Object is of SavingAccount,
// but type of object is Account(Parent Class)
// Upcasting - Create object of child class, but type of object is parent class
// when we do upcasting than we cannot access method of child class,
// we can access all methods of parent class and overrided methods in child class
Account acc = new SavingAccount();
acc.deposit(); // overrided method of SavingAccount
acc.withdraw(); // method of Account class
//acc.roi(); // method of SavingAccount
acc = new CurrentAccount();
acc.deposit();
acc.withdraw();
//acc.minBalance();
}
}