-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOPS.java
More file actions
659 lines (475 loc) · 13.6 KB
/
OOPS.java
File metadata and controls
659 lines (475 loc) · 13.6 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
// public class OOPS {
// public static void main(String args[]){
// // Pen p1 = new Pen(); // created a pen object p1
// // p1.setColor("Blue");
// // System.out.println(p1.color);
// // p1.setTip(5);
// // System.out.println(p1.tip);
// // p1.color = "Yellow";
// // System.out.println(p1.color);
// // BankAccount myAccount = new BankAccount();
// // myAccount.username = "MofizUsmani";
// // // myAccount.password = "abcdefghi";
// // myAccount.setPassword("abcdefghi");
// // System.out.println(myAccount.getPassword());
// // Pen p1 = new Pen();
// // p1.setColor("Green");
// // System.out.println(p1.getColor());
// // p1.setTip(10);
// // System.out.println(p1.getTip());
// // }
// // }
// // class Pen {
// // String color;
// // int tip;
// // String getColor(){
// // return this.color;
// // }
// // int getTip(){
// // return this.tip;
// // }
// // void setColor(String newColor){
// // this.color = newColor;
// // }
// // void setTip(int tip){
// // this.tip = tip;
// // }
// // }
// // class BankAccount {
// // public String username;
// // private String password;
// // public void setPassword(String pwd){
// // password = pwd;
// // }
// // // Getter to get Password
// // String getPassword(){
// // return this.password;
// // }
// // }
// // class Pen {
// // String color;
// // int tip;
// // void setColor(String newColor){
// // color = newColor;
// // }
// // void setTip(int newTip){
// // tip = newTip;
// // }
// // }
// // class Student {
// // String name;
// // int age;
// // float percentage;
// // void calcPercentage(int phy, int chem, int math) {
// // percentage = (phy + chem + math) / 3;
// // }
// // }
// Types of Constructors
// public class OOPS {
// public static void main(String[] args){
// Student s1 = new Student();
// Student s2 = new Student("Mofiz");
// Student s3 = new Student(12);
// System.out.println(s2.name);
// System.out.println(s3.roll);
// }
// }
// //Constructors
// class Student {
// String name;
// int roll;
// // Non-parametrazied constructor
// Student(){
// System.out.println("constructor is called...");
// }
// // Parametarized Constructor
// Student(String name){
// this.name = name;
// }
// // Parametarized Constructor
// Student(int roll){
// this.roll = roll;
// }
// }
//
// public class OOPS {
// public static void main (String[] args){
// Student s1 = new Student();
// s1.name = "mofiz";
// s1.roll = 12;
// s1.password = "abc";
// s1.marks[0] = 90;
// s1.marks[1] = 80;
// s1.marks[2] = 70;
// // Deep copy constructor - creates a new Student object by copying-
// // values from another Student object
// Student s2 = new Student(s1); //
// s1.marks[2] = 90;
// for(int i=0; i<3; i++){
// System.out.println(s2.marks[i]);
// }
// }
// }
// class Student {
// String name;
// int roll;
// String password;
// int marks[];
// // Shallow Copy Constructor
// // Student(Student s1){
// // marks = new int[3];
// // this.name = s1.name;
// // this.roll = s1.roll;
// // this.marks = s1.marks;
// // }
// // Deep Copy Constructor
// Student(Student s1){
// marks = new int[3];
// this.name = s1.name;
// this.roll = s1.roll;
// for(int i=0; i<marks.length; i++){
// this.marks[i] = s1.marks[i];
// }
// }
// Student(){
// marks = new int[3];
// System.out.println("Hello");
// }
// }
// // Inheritance (Single Level)
// public class OOPS {
// public static void main(String[] args){
// Fish shark = new Fish();
// shark.color = "Golden";
// shark.eat();
// System.out.println(shark.color);
// }
// }
// // Base Class
// class Animal {
// String color;
// void eat() {
// System.out.println("Eats");
// }
// void breathe() {
// System.out.println("Breathes");
// }
// }
// // Derived Class
// class Fish extends Animal {
// int fins;
// void swim(){
// System.out.println("Swims in water");
// }
// }
// Inheritance (Multilevel Inheritance)
// public class OOPS {
// public static void main(String[] args) {
// Shark s = new Shark();
// s.eat(); // from Animal
// s.swim(); // from Fish
// s.attack(); // from Shark
// }
// }
// // Base Class
// class Animal {
// void eat() {
// System.out.println("Eats food");
// }
// }
// // Derived Class 1
// class Fish extends Animal {
// void swim() {
// System.out.println("Swims in water");
// }
// }
// // Derived Class 2 (inherits Fish, which inherits Animal)
// class Shark extends Fish {
// void attack() {
// System.out.println("Attacks other fish");
// }
// }
// Inheritance (Hierarchial Inheritance)
// public class OOPS {
// public static void main(String[] args){
// Dog d = new Dog();
// d.eat(); // from Animal
// d.bark(); // from Dog
// Cat c = new Cat();
// c.eat(); // from Animal
// c.meow(); // from Cat
// Fish f = new Fish();
// f.eat(); // from Animal
// f.swim(); // from Fish
// }
// }
// // Base Class (Parent)
// class Animal {
// void eat() {
// System.out.println("Eats food");
// }
// }
// // Child 1
// class Dog extends Animal {
// void bark() {
// System.out.println("Barks loudly");
// }
// }
// // Child 2
// class Cat extends Animal {
// void meow() {
// System.out.println("Meows softly");
// }
// }
// // Child 3
// class Fish extends Animal {
// void swim() {
// System.out.println("Swims in water");
// }
// }
// Method Overloading
// public class OOPS {
// public static void main(String[] args) {
// Calculator c = new Calculator();
// System.out.println(c.add(5, 10)); // calls int version
// System.out.println(c.add(5.5, 10.5)); // calls double version
// }
// }
// class Calculator {
// int add(int a, int b) {
// return a + b;
// }
// double add(double a, double b) {
// return a + b;
// }
// }
// Method Overriding
// public class OOPS {
// public static void main(String[] args) {
// Animal a;
// a = new Animal();
// a.sound();
// a = new Dog();
// a.sound(); // Dog barks (runtime decision)
// a = new Cat();
// a.sound(); // Cat meows
// }
// }
// class Animal {
// void sound() {
// System.out.println("Animal makes a sound");
// }
// }
// class Dog extends Animal {
// void sound() {
// System.out.println("Dog barks");
// }
// }
// class Cat extends Animal {
// void sound() {
// System.out.println("Cat meows");
// }
// }
// User Defined Package
// import mypack.MyClass; // import the package
// class OOPS {
// public static void main(String[] args) {
// MyClass obj = new MyClass();
// obj.display();
// }
// }
// Abstraction in Java
// public class OOPS {
// public static void main(String[] args) {
// // Creating Horse object with default constructor
// Horse h1 = new Horse();
// h1.eat(); // Calls normal method from Animal
// h1.walk(); // Calls Horse's implementation of walk()
// // Creating Horse object with parameterized constructor
// Horse h2 = new Horse("Chetak");
// h2.eat();
// h2.walk();
// // Creating Chicken object with default constructor
// Chicken c1 = new Chicken();
// c1.eat();
// c1.walk();
// // Creating Chicken object with parameterized constructor
// Chicken c2 = new Chicken("Lucy");
// c2.eat();
// c2.walk();
// }
// }
// // ----------------- ABSTRACT CLASS -----------------
// abstract class Animal {
// String name;
// // Default constructor (no arguments)
// Animal() {
// System.out.println("Animal default constructor called");
// }
// // Parameterized constructor (initializes common property)
// Animal(String name) {
// this.name = name;
// System.out.println("Animal parameterized constructor: " + name);
// }
// // Normal (concrete) method
// void eat() {
// System.out.println("Animal Eats");
// }
// // Abstract method (must be implemented by subclasses)
// abstract void walk();
// }
// // ----------------- HORSE CLASS -----------------
// class Horse extends Animal {
// // Default constructor
// Horse() {
// // super() is automatically called here (Animal's default constructor)
// System.out.println("Horse constructor called");
// }
// // Parameterized constructor
// Horse(String name) {
// super(name); // Calls Animal(String name)
// System.out.println("Horse parameterized constructor called");
// }
// // Implementing abstract method
// void walk() {
// System.out.println("Horse walks on 4 legs");
// }
// }
// // ----------------- CHICKEN CLASS -----------------
// class Chicken extends Animal {
// // Default constructor
// Chicken() {
// // super() is automatically called here (Animal's default constructor)
// System.out.println("Chicken constructor called");
// }
// // Parameterized constructor
// Chicken(String name) {
// super(name); // Calls Animal(String name)
// System.out.println("Chicken parameterized constructor called");
// }
// // Implementing abstract method
// void walk() {
// System.out.println("Chicken walks on 2 legs");
// }
// }
// Interfaces in Java
// interface Animal {
// void eat();
// }
// interface Pet {
// void play();
// }
// class Dog implements Animal, Pet {
// public void eat() {
// System.out.println("Dog eats bone.");
// }
// public void play() {
// System.out.println("Dog plays fetch.");
// }
// }
// public class OOPS {
// public static void main(String[] args) {
// Dog d = new Dog();
// d.eat();
// d.play();
// }
// }
// Static Keyword
// public class OOPS {
// public static void main(String[] args) {
// Student s1 = new Student();
// s1.schoolName = "St.Xavier";
// Student s2 = new Student();
// System.out.println(s2.schoolName);
// Student s3 = new Student();
// s3.schoolName = "HEllo";
// System.out.println(s3.schoolName);
// System.out.println(s1.returnPercentage(60, 85, 90));
// }
// }
// class Student {
// // Static Method
// static int returnPercentage(int math, int phy, int chem){
// return (math + phy + chem) / 3;
// }
// String name;
// int roll;
// static String schoolName;
// void setName(String name){
// this.name = name;
// }
// String getName(){
// return this.name;
// }
// }
// Super Keyword (Access parent class variables (if child has same name))
// If a child class defines a variable with the same name as parent, super helps avoid ambiguity.
// class Animal {
// String color = "Brown";
// }
// class Dog extends Animal {
// String color = "Black";
// void printColor() {
// System.out.println(color); // Black (child)
// System.out.println(super.color); // Brown (parent)
// }
// }
// class OOPS {
// public static void main(String[] args) {
// Dog d = new Dog();
// d.printColor();
// }
// }
// Super Keyword (Call parent class constructor)
// super() is used to call the parent’s constructor. This must be the first statement inside child constructor.
// class Animal {
// Animal(String name) {
// System.out.println("Animal constructor: " + name);
// }
// }
// class Dog extends Animal {
// Dog(String name) {
// super(name); // call parent constructor
// System.out.println("Dog constructor: " + name);
// }
// }
// class OOPS {
// public static void main(String[] args) {
// Dog d = new Dog("Tommy");
// }
// }
// Super Keyword (Call parent class methods)
// If a child overrides a method, you can still call the parent’s method using super.
// class Animal {
// void eat() {
// System.out.println("Animal eats");
// }
// }
// class Dog extends Animal {
// void eat() {
// System.out.println("Dog eats bones");
// }
// void print() {
// eat(); // Calls Dog's method (overridden)
// super.eat(); // Calls Animal's method
// }
// }
// public class OOPS {
// public static void main(String[] args) {
// Dog d = new Dog(); // Create Dog object
// d.print(); // Call print()
// }
// }
// Practice Question 1:
// class Student {
// String name;
// int marks;
// }
// public class OOPS {
// public static void main(String[] args) {
// Student c = new Student();
// c.name = "Jack";
// System.out.println(c.name);
// }
// }
// Practice Question 2: