-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOPs
More file actions
90 lines (68 loc) · 1.36 KB
/
OOPs
File metadata and controls
90 lines (68 loc) · 1.36 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
### Encapsulation
class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
}
### inheritance
class Vehicle {
int speed = 60;
void move() {
System.out.println("Vehicle is moving");
}
}
class Car extends Vehicle {
void fuel() {
System.out.println("Car uses petrol");
}
}
class Demo {
public static void main(String[] args) {
Car c = new Car();
c.move();
c.fuel();
System.out.println(c.speed);
}
}
###polymorphism
class Payment {
void pay() {
System.out.println("Payment method");
}
}
class UPI extends Payment {
void pay() {
System.out.println("Paying using UPI");
}
}
class Card extends Payment {
void pay() {
System.out.println("Paying using Card");
}
}
class Demo {
public static void main(String[] args) {
Payment p;
p = new UPI();
p.pay();
p = new Card();
p.pay();
}
}
#### abstraction
class UPI implements Payment {
public void pay() {
System.out.println("Paying via UPI");
}
}
class Card implements Payment {
public void pay() {
System.out.println("Paying via Card");
}
}