-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoops.java
More file actions
454 lines (340 loc) · 11.3 KB
/
oops.java
File metadata and controls
454 lines (340 loc) · 11.3 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
// Learning OOPs concepts in Java
// OOPs stands for Object-Oriented Programming System. It is a programming paradigm that uses objects and classes to design and develop applications. The main concepts of OOPs are:
// ==================================================
// 1️⃣ CLASSES & OBJECTS, //attributes
// ==================================================
// 1. classes & objects:- class is a blueprint from which individual objects are created and an object is an instance of a class. It is a real-world entity that has state and behavior.
// For example, a car can be an object of the class "Vehicle" which has properties like color, model, and methods like start() and stop().
//class → defines actions ; object → calls those actions
//creating class with obj
class Dog {
// Declaring and initializing the attributes
String breed; // String breed = "German Shepherd";
int age; // or int age = 2; to modify-> obj.age = 3;
String color;
final String name = "Tommy"; // cant modify
// methods to set breed, age, and color of the dog
void setBreed(String breed) {
this.breed = breed;
}
void setAge(int age) {
this.age = age;
}
void setColor(String color) {
this.color = color;
}
// method to print all three values
void printDetails() {
System.out.println("Dog details:");
System.out.println(this.breed);
System.out.println(this.age);
System.out.println(this.color);
}
}
public class oops {
public static void main(String[] args) {
// Syntax to Create a Java Object - Class_name object_name = new Class_name([parameters]);
// Creating an object of the class Dog
Dog obj = new Dog();
// setting the attributes
obj.setBreed("Golden Retrives");
obj.setAge(2);
obj.setColor("Golden");
obj.printDetails();
}
}
// ✨ Instance variables and methods belong to objects, so we access them using the object name and dot operator.
// set to set it, get to print from it, & if directly want -> Access Variable directly= objectName.variableName
class Puppy {
int puppyAge;
public Puppy(String name) {
// This constructor has one parameter, <i>name</i>.
System.out.println("Name chosen is :" + name);
}
public void setAge(int age) {
puppyAge = age;
}
public int getAge() {
System.out.println("Puppy's age is :" + puppyAge);
return puppyAge;
}
public static void main(String[] args) {
/* Object creation */
Puppy myPuppy = new Puppy("tommy");
/* Call class method to set puppy's age */
myPuppy.setAge(2);
/* Call another class method to get puppy's age */
myPuppy.getAge();
/* You can access instance variable as follows as well */
System.out.println("Variable Value :" + myPuppy.puppyAge);
}
}
//example to demonstrate how to define class method and how to access it
//✨ Define method inside class → create object → call method using object.method().
class Cat {
// class method
void meow() {
System.out.println("Cat is meowing");
}
public static void main(String[] args) {
// create object
Cat c = new Cat();
// access the class method using object
c.meow();
}
}
// ==================================================
// ✳️ METHODS
// ==================================================
// A Java method is a block of code inside a class that performs a specific task and runs when it is called. 🚀
//Syntax to Create a Java Method-> modifier returnType nameOfMethod (Parameter List) {//}
//ex Defining and Calling a Java Method
class ExampleMinNumber {
public static void main(String[] args) {
int a = 11;
int b = 6;
int c = minFunction(a, b);
System.out.println("Minimum Value = " + c);
}
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
//1. The void Keyword with Java Methods
class ExampleVoid {
public static void main(String[] args) {
methodRankPoints(255.7);
}
public static void methodRankPoints(double points) {
if (points >= 202.5) {
System.out.println("Rank:A1");
} else if (points >= 122.4) {
System.out.println("Rank:A2");
} else {
System.out.println("Rank:A3");
}
}
}
//2. Passing Parameters by Value in Java Methods
//Definition: Passing by value means a copy of the argument value is passed to the method, so the original variable remains unchanged.
//Example: swapFunction(a, b);
//3. Java Methods Overloading
//Definition: Method overloading means multiple methods having the same name but different parameter types or numbers.
//Example: minFunction(int n1, int n2) and minFunction(double n1, double n2)
//*// variable scope as “where a variable can be used and how long it lives.” 🌱
//| Type | Declared | Lifetime | Access |
//| ------------ | -------------------------- | ------------------- | ---------------------- |
//| **Instance** | Inside class | Until object exists | `object.variable` |
//| **Static** | Inside class with `static` | Whole program | `ClassName.variable` |
//| **Local** | Inside method | Until method ends | Only inside that block |
// ==================================================
// ✳️ CONSTRUCTORS
// ==================================================
// const name should be same as cls name, no return type
// syx - class ClassName {ClassName() { }}
// types -> 1. Default Constructor (A Class Without Any Constructor)
//2. No-Args Constructor
class Main {
int num1;
int num2;
// Creating no-args constructor
Main() {
num1 = -1;
num2 = -1;
}
public static void main(String[] args) {
// no-args constructor will invoke
Main obj_x = new Main();
// Printing the values
System.out.println("num1 : " + obj_x.num1);
System.out.println("num2 : " + obj_x.num2);
}
}
//3. Parameterized Constructor
class Mainn {
int num1;
int num2;
// Creating parameterized constructor
Mainn(int a, int b) {
num1 = a;
num2 = b;
}
public static void main(String[] args) {
// Creating two objects by passing the values
// to initialize the attributes.
// parameterized constructor will invoke
Mainn obj_x = new Mainn(10, 20);
Mainn obj_y = new Mainn(100, 200);
// Printing the objects values
System.out.println("obj_x");
System.out.println("num1 : " + obj_x.num1);
System.out.println("num2 : " + obj_x.num2);
System.out.println("obj_y");
System.out.println("num1 : " + obj_y.num1);
System.out.println("num2 : " + obj_y.num2);
}
}
// ==================================================
// ✳️ ACCESS MODIFIERS
// ==================================================
// decide who can see or use a variable, method, class, or constructor. 🔐
// ==================================================
// ✳️ INHERITANCE
// ==================================================
// ✨ Inheritance lets a child class reuse and extend the features of a parent class using extends
// Parent class
class Animal{
String name;
//Constructor
Animal(String name){
this.name = name;
}
//Parent method
void eat(){
System.out.println(name + " is eating");
}
void sound(){
System.out.println("Animal makes sound");
}
}
// Child class
class crow extends Animal {
crow(String name){
super(name);// calls parent constructor
}
//New method (child feature)
void run(){
System.out.println(name + " is running");
}
//Method overriding
void sound(){
System.out.println(name + " croo");
}
}
//Main class
/*public*/ class Mian{
public static void main(String[] args){
//Creating child object
crow c = new crow("kowa");
// Inherited method from parent
c.eat();
// Overridden method
c.sound();
// Child method
c.run();
}
}
// ==================================================
// ✳️ AGGREGATION
// ==================================================
class Address {
String city;
String state;
Address(String city, String state){
this.city = city;
this.state = state;
}
}
class Student {
int rollNo;
String name;
Address address; // Aggregation happens here
Student(int rollNo, String name, Address address) {
this.rollNo = rollNo;
this.name = name;
this.address = address;
}
void display() {
System.out.println(rollNo + " " + name);
System.out.println(address.city + " " + address.state);
}
}
/* public class Main {
public static void main(String[] args) {
Address ad = new Address("Hyderabad", "Telangana");
Student s = new Student(101, "Riya", ad);
s.display();
}
}*/
// ==================================================
// ✳️ POLYMORPHISM- Overloading , Overriding
// ==================================================
//Method Overloading (same method name, different parameters)
//Method Overriding (child changes parent method)
//Polymorphism (parent reference → child object)
class Animall {
// Method (will be overridden)
void sound(){
System.out.println("Animal makes a sound");
}
// Method Overloading
void eat(){
System.out.println("Animal is eating");
}
void eat(String food){
System.out.println("Animal eats" + food);
}
}
class Lion extends Animall {
// Method Overriding
void sound(){
System.out.println("Lion roars");
}
}
// public class Main {
// public static void main(String[] args) {
// // Polymorphism
// Animal a = new Dog();
// // Overriding happens here
// a.sound();
// // Overloading examples
// a.eat();
// a.eat("meat");
// }
// }
// ==================================================
// ✳️ ABSTRACTION & Encapsulation
// ==================================================
//You see only the important part, not the complex internal process. hides implementation details and shows only functionality.
abstract class Aniimal {
// Encapsulation (private variables)
private String name;
private int age;
// Setter methods
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
// Getter methods
public String getName() {
return name;
}
public int getAge() {
return age;
}
// Abstract method (Abstraction)
abstract void makeSound();
}
class Doog extends Aniimal {
// Implementing abstract method
void makeSound() {
System.out.println("Dog barks 🐶");
}
}
// public class Main {
// public static void main(String[] args) {
// Dog d = new Dog();
// d.setName("Rocky");
// d.setAge(3);
// System.out.println("Dog Name: " + d.getName());
// System.out.println("Dog Age: " + d.getAge());
// d.makeSound();
// }
// }