You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+43-8
Original file line number
Diff line number
Diff line change
@@ -29,9 +29,7 @@ In this Code,there are 5 identifiers:
29
29
30
30
There are **53** reserved words in Java of which **50** are **keywords** and **3** are **literals**(**true,false,null**)
31
31
32
-
# OOP CONCEPTS:
33
-
34
-
**CLASS:**
32
+
# CLASS:
35
33
36
34
A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order:
37
35
@@ -45,7 +43,7 @@ A class is a user defined blueprint or prototype from which objects are created.
45
43
46
44
Body: The class body surrounded by braces, { }.
47
45
48
-
**OBJECT:**
46
+
# OBJECT:
49
47
50
48
An object is an instance of a class.Technically, Class is a template which describes what state and behavior of an instance this class can have. Object implements the state and behavior in the form of variables and methods and requires some memory allocated.An object consists of:
51
49
@@ -214,7 +212,8 @@ The **java.lang.System.exit()** method exits current program by terminating runn
214
212
# CONSTRUCTORS:
215
213
216
214
Constructors are used for initializing new objects. Constructors does not return any values but implicitly it returns the object of the class. Fields are variables that provides the state of the class and its objects, and methods are used to implement the behavior of the class and its objects.
@@ -228,7 +227,40 @@ Java creates a default constructor automatically if no default or parameterized
228
227
229
228
**Copy Constructor:**
230
229
Java supports copy constructor but doesn't create a default copy of constructor if user doesn't create one.
231
-
**Code2:CopyConstructor.java:**
230
+
231
+
**Code2:[CopyConstructor.java](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/blob/master/Constructors/CopyConstructor.java):** A program to illustrate the use of copy constructor
232
+
233
+
### CONSTRUCTOR CHAINING:
234
+
235
+
Constructor chaining is the process of calling one constructor from another constructor with respect to current object.
236
+
Constructor chaining can be done in two ways:
237
+
238
+
1.Within same class: It can be done using this() keyword for constructors in same class.See [this2.java](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/blob/master/this%20keyword/this2.java)
239
+
240
+
2.From base class: by using super() keyword to call constructor from the base class.See [superWithConstructor.java](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/blob/master/Super%20keyword/superWithConstructor.java)
241
+
242
+
This process is used when we want to perform multiple tasks in a single constructor. Rather than creating a code for each task in a single constructor we create a separate constructor for each task and make their chain which makes the program more readable.
243
+
244
+
**Points to be noted:**
245
+
246
+
1.The this() expression should always be the first line of the constructor.
247
+
2.There should be at-least be one constructor without the this() keyword (constructor 3 in above example).
248
+
3.Constructor chaining can be achieved in any order.
249
+
250
+
### PRIVATE CONSTRUCTORS AND SINGLETON CLASS:
251
+
252
+
We can provide access specifier to the constructor. If made private, then it can only be accessed inside the class.
253
+
We can use private constructors for 1. Internal Constructor Chaining 2. Singleton class design
254
+
255
+
A singleton class is a class that can have not more than a single object.
256
+
After first time, if we try to instantiate the Singleton class, the new variable also points to the first instance created. So whatever modifications we do to a variable inside the class through any instance, it affects the variable of the single instance created and is visible if we access that variable through any instance of that class.
257
+
258
+
**Code3:[PrivateConstructor.java](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/blob/master/Constructors/PrivateConstructor.java):** A program to illustrate singleton class using Private constructor.
259
+
260
+
To design a singleton class:
261
+
262
+
1.Make constructor as private.
263
+
2.Write a static method that has return type object of this singleton class. Here, the concept of Lazy initialization in used to write this static method.
232
264
233
265
## CONSTRUCTORS VS METHODS:
234
266
@@ -248,6 +280,9 @@ By contrast, Methods cannot be used to create an Instance of a Class.
248
280
249
281
### CONSTRUCTOR OVERLOADING:
250
282
283
+
In java,it is possible to define two or more constructor of the same class that obviously share the same name but their parameter declaration are different either in terms of number of parameters or type of the parameters or both.This process is called Constructor Overloading.
284
+
285
+
251
286
252
287
**JAVA ACCESS MODIFIERS:**
253
288
@@ -300,7 +335,7 @@ Multiple inheritance is not supported by Java using classes , handling the compl
300
335
301
336
**Code1: [this1.java](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/blob/master/this%20keyword/this1.java):** this used to refer current class instance variables.
302
337
303
-
**Code2: [this2.java](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/blob/master/this%20keyword/this2.java):** used to invoke current class constructor.
338
+
**Code2: [this2.java](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/blob/master/this%20keyword/this2.java):** used to invoke current class constructor.This code is an example of **Constructor chaining using this()** discussed above.
304
339
305
340
**Code3: [this3.java](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/blob/master/this%20keyword/this3.java):** used to invoke current class instances.
306
341
@@ -316,7 +351,7 @@ The super keyword in java is a reference variable that is used to refer parent c
**Code1:[superWithConstructor.java](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/blob/master/Super%20keyword/superWithConstructor.java):** 'super' keyword can also be used to access the parent class constructor.'super' can call both parametric as well as non parametric constructors depending upon the situation.
354
+
**Code1:[superWithConstructor.java](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/blob/master/Super%20keyword/superWithConstructor.java):** 'super' keyword can also be used to access the parent class constructor.'super' can call both parametric as well as non parametric constructors depending upon the situation.This code is also an example of **Constructor chaining with super()** discussed above.
320
355
321
356
**Code2:[superWithMethods.java](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/blob/master/Super%20keyword/superWithMethods.java):** A program to illustrate that whenever a parent and child class have same named methods then to resolve ambiguity we use super keyword.
0 commit comments