Skip to content

Commit faaf481

Browse files
authored
Update README.md
1 parent 9f829bf commit faaf481

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

README.md

+43-8
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ In this Code,there are 5 identifiers:
2929

3030
There are **53** reserved words in Java of which **50** are **keywords** and **3** are **literals**(**true,false,null**)
3131

32-
# OOP CONCEPTS:
33-
34-
**CLASS:**
32+
# CLASS:
3533

3634
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:
3735

@@ -45,7 +43,7 @@ A class is a user defined blueprint or prototype from which objects are created.
4543

4644
Body: The class body surrounded by braces, { }.
4745

48-
**OBJECT:**
46+
# OBJECT:
4947

5048
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:
5149

@@ -214,7 +212,8 @@ The **java.lang.System.exit()** method exits current program by terminating runn
214212
# CONSTRUCTORS:
215213

216214
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.
217-
**Folder:[Constructors](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/tree/master/Constructors)
215+
216+
**Folder:[Constructors](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/tree/master/Constructors)**
218217

219218
**Constructors cannot be Inherited:**
220219

@@ -228,7 +227,40 @@ Java creates a default constructor automatically if no default or parameterized
228227

229228
**Copy Constructor:**
230229
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.
232264

233265
## CONSTRUCTORS VS METHODS:
234266

@@ -248,6 +280,9 @@ By contrast, Methods cannot be used to create an Instance of a Class.
248280

249281
### CONSTRUCTOR OVERLOADING:
250282

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+
251286

252287
**JAVA ACCESS MODIFIERS:**
253288

@@ -300,7 +335,7 @@ Multiple inheritance is not supported by Java using classes , handling the compl
300335

301336
**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.
302337

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.
304339

305340
**Code3: [this3.java](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/blob/master/this%20keyword/this3.java):** used to invoke current class instances.
306341

@@ -316,7 +351,7 @@ The super keyword in java is a reference variable that is used to refer parent c
316351

317352
**FOLDER:[super KEYWORD](https://github.com/disha2sinha/Object-Oriented-Programming-in-Java/tree/master/Super%20keyword):**
318353

319-
**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.
320355

321356
**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.
322357

0 commit comments

Comments
 (0)