Repository navigation
Expand file tree
/
Copy path14_OOP_concepts.js
More file actions
232 lines (191 loc) · 5.59 KB
/
Copy path14_OOP_concepts.js
File metadata and controls
232 lines (191 loc) · 5.59 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// it covers the five main pillars of OOP concepts: Encapsulation, Abstraction, Inheritance, Polymorphism, and Composition.
// classes and objects are the building blocks of OOP, and they allow developers to create modular and reusable code.
//1. classes and objects
class Student {
static collageName = " ACE collage "
static totalStudents = 0;
constructor(name, marks){
this.name = name;
this._marks = marks;
Student.totalStudents++;
}
// getter method access without () .
get grade(){
if(this._marks >= 90) return "A+";
if(this._marks >= 75) return "A";
if(this._marks >= 60) return "C";
if(this._marks >= 50) return "D";
return "fail";
}
// setter method - validation at the time of initialization
set marks(newMarks){
if(newMarks < 0 || newMarks > 100 ){
console.log("submit the marks between 0 to 100 ");
return ;
}
this._marks = newMarks;
}
introduce (){
console.log(` hey!, i am ${this.name} , Grade: ${this.grade}`);
}
static collageInfo(){
console.log(` collega :${Student.collageName} and totalStudents :${Student.totalStudents}`);
}
}
//2. Encapsulation - wrapup the data into one class
class BankAccount{
#Balence;
constructor(owner , initialBalence = 0){
this.owner = owner;
this.#Balence = initialBalence;
}
deposit(amount){
if( amount <= 0 ){
console.log("deposit a positve amount , procces failed ");
return;
}
this.#Balence += amount;
console.log(`${amount} deposit successfully , current balence ${this.#Balence}`);
}
withdraw(amount){
if(amount >this.#Balence || amount <=0){
console.log("insuffient balence ! please enter a valid money ");
return;
}
this.#Balence -= amount;
console.log(`${amount} withdraw successfully , current balence ${this.#Balence}`)
}
get getBalence(){
return this.#Balence;
}
}
//3. Inhertance - child class inherit the properties and methods of parent class
class Vehicle{
constructor(brand, speed){
this.brand = brand;
this.speed = speed;
}
info(){
console.log(` Brand :${this.brand} top speed of vehicle ${this.speed}.`);
}
honk(){
console.log("Beep Beep!");
}
}
class Car extends Vehicle{
constructor(brand , speed , doors){
super(brand,speed );
this.doors = doors;
}
info(){
super.info();
console.log(`this car has ${this.doors}`);
}
}
class electricBike extends Vehicle{
constructor(brand, speed , batteryRange){
super(brand,speed);
this.batteryRange = batteryRange;
}
info(){
super.info();
console.log(` battery range is ${this.batteryRange}`);
}
}
//4. class for the example three - polymorphism
class Shape {
area(){
console.log("this is a function for area .");
}
}
class Circle extends Shape {
constructor(radius){
super();
this.radius = radius;
}
area(){
const result = Math.PI*this.radius**2;
console.log("the area of a circle : ",(result.toFixed(2)));
}
}
class Ractangle extends Shape{
constructor(width , height){
super();
this.width = width;
this.height = height;
}
area(){
console.log(`Area of ractangle : ${this.height * this.width}`);
}
}
class Triangle extends Shape {
constructor(base , height ){
super();
this.base = base ;
this.height = height;
}
area(){
console.log(` Area of triangle : ${this.base * this.height /2}`);
}
}
//5. class for the exapmle 5 - Abstraction
class PaymenPathWay {
constructor(){
if(this.constructor === PaymenPathWay){
throw new Error(`paymentpathway is not directly accesible `);
}
}
processPayment (amount){ // abstract method
throw new Error("define proccesspayment method in the child class");
}
initiate(amount){
console.log("payment is initiating ",amount);
this.processPayment(amount);
console.log("payment completed ");
}
}
class UpiPayment extends PaymenPathWay{
processPayment(amount){
console.log(`payment ${amount} is in proccesssing through UPI`);
}
}
class CardPayment extends PaymenPathWay{
processPayment(amount){
console.log(`payment ${amount} is in proccesssing through Card`);
}
}
// class call for example one( student )
const s1 = new Student("Aman" , 78);
const s2 = new Student("Rahul" , 87);
s1.introduce ();
s2.introduce ();
s1.marks = 150; // show the rejection
s1.marks = 79;
s1.introduce();
Student.collageInfo();
// class call for example 2 (Bank Account )
const x1 = new BankAccount("Aman " , 1000);
x1.deposit(1000);
x1.withdraw(500);
console.log("Final balence ", x1.getBalence);
// class object call for the example 3
const myCar = new Car("tata ", 110, 5);
myCar.info();
myCar.honk();
console.log(" --- ");
const myBike = new electricBike("ola" , 90 , "180km");
myBike.info();
// class object call for the example 4
const myShape = new Shape();
myShape.area();
const myCircle = new Circle(5);
myCircle.area();
const myRactangle = new Ractangle(4,6);
myRactangle.area();
const myTriangle = new Triangle( 6 , 5);
myTriangle.area();
// class object call for the example 5
const upipay = new UpiPayment();
upipay.initiate(499);
const cardpay = new CardPayment();
cardpay.initiate(599);