Skip to content

Commit da81742

Browse files
committed
added constructors
1 parent 4bc5302 commit da81742

File tree

3 files changed

+240
-3
lines changed

3 files changed

+240
-3
lines changed

.idea/workspace.xml

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ It is used to develop rich internet applications. It uses a light-weight user in
9797
|[Iterative Statements](https://github.com/connectaman/Java_Notes_and_Programs/tree/master/src/IterativeStatements)|
9898
|[Oops Concept](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/OopsConcept/Oops.md)|
9999
|[Naming Convenction](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/OopsConcept/NamingConventions.md)|
100-
|[Object and Classes]()|
101-
|[]()|
100+
|[Object and Classes](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/OopsConcept/ObjectsandClasses.md)|
101+
|[Constructors]()|
102102
|[]()|
103103
|[]()|
104104

src/OopsConcept/Constructors.md

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
### Constructors in Java
2+
3+
-----
4+
5+
In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory.
6+
7+
It is a special type of method which is used to initialize the object.
8+
9+
Every time an object is created using the new() keyword, at least one constructor is called.
10+
11+
It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default.
12+
13+
There are two types of constructors in Java: no-arg constructor, and parameterized constructor.
14+
15+
Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn't have any.
16+
17+
------
18+
19+
##### Rules for creating Java constructor
20+
21+
There are two rules defined for the constructor.
22+
23+
1. Constructor name must be the same as its class name
24+
2. A Constructor must have no explicit return type
25+
3. A Java constructor cannot be abstract, static, final, and synchronized
26+
27+
-----
28+
29+
##### Types of Java constructors
30+
31+
There are two types of constructors in Java:
32+
33+
1. Default constructor (no-arg constructor)
34+
2. Parameterized constructor
35+
36+
------
37+
38+
![](https://static.javatpoint.com/images/core/java-constructor.png)
39+
40+
----
41+
42+
##### Java Default Constructor
43+
44+
A constructor is called "Default Constructor" when it doesn't have any parameter.
45+
46+
Example of default constructor
47+
In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation.
48+
49+
```java
50+
class Bike1{
51+
//creating a default constructor
52+
Bike1(){System.out.println("Bike is created");}
53+
//main method
54+
public static void main(String args[]){
55+
//calling a default constructor
56+
Bike1 b=new Bike1();
57+
}
58+
}
59+
```
60+
Output
61+
```
62+
Bike is created
63+
```
64+
65+
-----
66+
67+
Example of default constructor that displays the default values
68+
69+
```java
70+
class Student3{
71+
int id;
72+
String name;
73+
//method to display the value of id and name
74+
void display(){System.out.println(id+" "+name);}
75+
76+
public static void main(String args[]){
77+
//creating objects
78+
Student3 s1=new Student3();
79+
Student3 s2=new Student3();
80+
//displaying values of the object
81+
s1.display();
82+
s2.display();
83+
}
84+
}
85+
```
86+
Output
87+
```
88+
0 null
89+
0 null
90+
```
91+
92+
--------
93+
94+
#### Java Parameterized Constructor
95+
96+
A constructor which has a specific number of parameters is called a parameterized constructor.
97+
##### Why use the parameterized constructor?
98+
99+
The parameterized constructor is used to provide different values to distinct objects. However, you can provide the same values also.
100+
101+
Example of parameterized constructor
102+
103+
In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.
104+
105+
```java
106+
class Student4{
107+
int id;
108+
String name;
109+
//creating a parameterized constructor
110+
Student4(int i,String n){
111+
id = i;
112+
name = n;
113+
}
114+
//method to display the values
115+
void display(){System.out.println(id+" "+name);}
116+
117+
public static void main(String args[]){
118+
//creating objects and passing values
119+
Student4 s1 = new Student4(111,"Karan");
120+
Student4 s2 = new Student4(222,"Aryan");
121+
//calling method to display the values of object
122+
s1.display();
123+
s2.display();
124+
}
125+
}
126+
```
127+
Output
128+
```
129+
111 Karan
130+
222 Aryan
131+
```
132+
133+
-----
134+
135+
#### Constructor Overloading in Java
136+
137+
In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods.
138+
139+
Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler by the number of parameters in the list and their types.
140+
141+
```java
142+
class Student5{
143+
int id;
144+
String name;
145+
int age;
146+
//creating two arg constructor
147+
Student5(int i,String n){
148+
id = i;
149+
name = n;
150+
}
151+
//creating three arg constructor
152+
Student5(int i,String n,int a){
153+
id = i;
154+
name = n;
155+
age=a;
156+
}
157+
void display(){System.out.println(id+" "+name+" "+age);}
158+
159+
public static void main(String args[]){
160+
Student5 s1 = new Student5(111,"Karan");
161+
Student5 s2 = new Student5(222,"Aryan",25);
162+
s1.display();
163+
s2.display();
164+
}
165+
}
166+
```
167+
Output
168+
```
169+
111 Karan 0
170+
222 Aryan 25
171+
```
172+
173+
------
174+
175+
|Java Constructor | Java Method|
176+
|-------|----|
177+
|A constructor is used to initialize the state of an object. |A method is used to expose the behavior of an object.|
178+
|A constructor must not have a return type.| A method must have a return type.|
179+
|The constructor is invoked implicitly.| The method is invoked explicitly.|
180+
|The Java compiler provides a default constructor if you don't have any constructor in a class.| The method is not provided by the compiler in any case.|
181+
|The constructor name must be same as the class name.| The method name may or may not be same as the class name.|
182+
183+
------
184+
185+
![](https://static.javatpoint.com/images/constructor-vs-method-in-java.jpg)
186+
187+
-------
188+
189+
##### Java Copy Constructor
190+
191+
There is no copy constructor in Java. However, we can copy the values from one object to another like copy constructor in C++.
192+
193+
There are many ways to copy the values of one object into another in Java. They are:
194+
195+
-By constructor
196+
-By assigning the values of one object into another
197+
-By clone() method of Object class
198+
199+
In this example, we are going to copy the values of one object into another using Java constructor.
200+
201+
```java
202+
class Student6{
203+
int id;
204+
String name;
205+
//constructor to initialize integer and string
206+
Student6(int i,String n){
207+
id = i;
208+
name = n;
209+
}
210+
//constructor to initialize another object
211+
Student6(Student6 s){
212+
id = s.id;
213+
name =s.name;
214+
}
215+
void display(){System.out.println(id+" "+name);}
216+
217+
public static void main(String args[]){
218+
Student6 s1 = new Student6(111,"Karan");
219+
Student6 s2 = new Student6(s1);
220+
s1.display();
221+
s2.display();
222+
}
223+
}
224+
```
225+
Ouput
226+
```
227+
111 Karan
228+
111 Karan
229+
```
230+
231+
------
232+
233+
234+
235+

0 commit comments

Comments
 (0)