-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfucntionsAndClass.java
372 lines (308 loc) · 8.18 KB
/
fucntionsAndClass.java
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
// fucntions
// class
//They are defined as member variables and member functions
class Calculator
{
public int add(int num1, int num2){
System.out.println("I am in function method");
return num1 + num2;
}
}
class Main{
public static void main(String a[]){
int num1 = 2; // int is a primitive type. So, num1, num2 are primitive variable
int num2 = 6;
Calculator cal = new Calculator(); // cal is reference variable, "new Calculator()" creating a new object of Calculator type
int result = cal.add(num1,num2);
System.out.println(result);
}
}
// difference between void and datatypes
class OrderHere{
public void boba() // void - it does not return any value
{
System.out.println("Boba takes 5 mins to get ready");
}
public String order(int cost, String name) // String - it returns a string value
{
if (cost >= 10)
return String.format("your %s is ready", name);
return "sorry! Need more money to place an order";
}
}
class Main{
public static void main(String a[]){
OrderHere order = new OrderHere();
order.boba();
String result = order.order(15, "boba tea");
System.out.println(result);
}
}
// Method overloading - functions having the same name but different numbers of variables and other data types.
class Calculator
{
public int add(int num1, int num2)
{
return num1 + num2;
}
public int add(int num1, int num2, int num3)
{
return num1 + num2 + num3;
}
public double add(double num1, int num2)
{
return num1 + num2;
}
}
class Main
{
public static void main(String a[])
{
Calculator obj = new Calculator();
int result = obj.add(6,1,5);
System.out.println(result);
}
}
// static methods/variables are accessed by class, whereas instance methods/variables are accessed by objects(created by class)
// static variable
class Mobile
{
String brand;
int cost;
static String name; // static variable
public void show() // instance method
{
System.out.println(brand + " " + cost + " " + name);
}
}
class Main
{
public static void main(String a[])
{
Mobile obj = new Mobile();
obj.brand = "Apple";
obj.cost = 1000;
Mobile.name = "smartphone";
Mobile obj2 = new Mobile();
obj2.brand = "Google";
obj2.cost = 900;
System.out.println("First Product");
obj.show();
System.out.println("Second Product");
obj2.show();
}
}
// static methods
// static method
class Mobile
{
String brand;
int cost;
static String name;
public void show() // instance method
{
System.out.println(brand + " " + cost + " " + name);
}
public static void show1(Mobile obj) // static method
{
System.out.println(obj.brand + " " + obj.cost + " " + name); // cannot access instance variables directly in static methods
}
}
class Main
{
public static void main(String a[])
{
Mobile obj = new Mobile();
obj.brand = "Apple";
obj.cost = 1000;
Mobile.name = "smartphone";
Mobile obj2 = new Mobile();
obj2.brand = "Google";
obj2.cost = 900;
System.out.println("First Product");
obj.show();
System.out.println("Second Product");
obj2.show();
Mobile.show1(obj); // calling a static method, passing an argument with an obj
}
}
// static block
// Whenever we create a class, it has 2 steps
// class loads
// object gets initialized
// static block - it is called when we create a class
// constructor - it is called at the time of object creation
// name of the constructor should be the same as the name of the class
// static method
class Mobile
{
String brand;
int cost;
static String name;
static // static block
{
name = "smartphone";
System.out.println("I am in static method");
}
public Mobile() // constructor
{
brand = "";
cost = 200;
System.out.println("I am in constructor");
}
public void show()
{
System.out.println(brand + " " + cost + " " + name);
}
}
class Main
{
public static void main(String a[])
{
Mobile obj = new Mobile();
obj.brand = "Apple";
obj.cost = 1000;
Mobile.name = "smartphone";
Mobile obj2 = new Mobile();
obj2.brand = "Google";
obj2.cost = 900;
System.out.println("First Product");
obj.show();
System.out.println("Second Product");
obj2.show();
}
}
// If you don't create the object, it will not load the class as well
// but what if we want to load the class
//In Java, we have Class.forName("nameOfYourClass") to load the class; when we use Class, we need to define the expectation
class Mobile
{
String brand;
int cost;
static String name;
static
{
name = "smartphone";
System.out.println("I am in static method");
}
public Mobile()
{
brand = "";
cost = 200;
System.out.println("I am in constructor");
}
public void show()
{
System.out.println(brand + " " + cost + " " + name);
}
}
class Main
{
public static void main(String a[]) throws ClassNotFoundException
{
Class.forName("Mobile");
}
private static class classNotFoundException extends Exception {
}
}
// Encapsulation
// Definition: where we bind both the member variables and member functions together is called encapsulation
// private variables can be accessed by their own class or its methods
class Details
{
private String name = "Anu"; // private variables
private int age = 26;
public String getName() // getter methods
{
return name;
}
public int getAge() // getter methods
{
return age;
}
public void setName(String s)
{
name = s;
}
public void setAge(int a)
{
age = a;
}
}
class Main
{
public static void main(String a[])
{
Details person = new Details();
person.setName("chinnu"); // to set a value
person.setAge(26);
System.out.println(person.getName());
System.out.println(person.getAge());
}
}
// this keyword
//This keyword is generally used to refer to the current object
class Details
{
private String name = "Anu"; // private variables
private int age = 26;
public String getName() // getter methods
{
return name;
}
public int getAge() // getter methods
{
return age;
}
public void setName(String name) // here name is local variable
{
this.name = name; // this keyword
} // when we
public void setName(String name) // error from line 315 - 318
{
name = name; // when we define name = name;
}
public void setAge(int age)
{
this.age = age;
}
}
class Main
{
public static void main(String a[])
{
Details person = new Details();
person.setName("chinnu");
person.setAge(26);
System.out.println(person.getName());
System.out.println(person.getAge());
}
}
// default and parameterized constructor
class Order
{
String flavour;
int cost;
public Order() // default constructor
{
flavour = "cotton candy";
cost = 15;
}
public Order(String flavour, int cost) // parameterized constructor
{
this.flavour = flavour;
this.cost = cost;
}
}
class Main
{
public static void main(String a[])
{
Order order1 = new Order();
Order order2 = new Order("black grape", 20);
System.out.println("welcome to lolas yogourt");
System.out.println(order1.flavour + " - " + order1.cost);
System.out.println(order2.flavour + " - " + order2.cost);
}
}
// Inheritance