Skip to content

Commit cca16f1

Browse files
Constructors in Java
1 parent e5bb4da commit cca16f1

File tree

1 file changed

+211
-0
lines changed

1 file changed

+211
-0
lines changed

42.Constructors/README.md

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
# Java Tutorial: Constructors in Java
2+
3+
### Constructors in Java :
4+
- Constructors are similar to methods,, but they are used to initialize an object.
5+
- Constructors do not have any return type(not even void).
6+
- Every time we create an object by using the new() keyword, a constructor is called.
7+
- If we do not create a constructor by ourself, then the default constructor(created by Java compiler) is called.
8+
9+
### Rules for creating a Constructor :
10+
1. The class name and constructor name should be the same.
11+
2. It must have no explicit return type.
12+
3. It can not be abstract, static, final, and synchronized.
13+
14+
### Types of Constructors in Java :
15+
- There are two types of constructors in Java :
16+
17+
**1. Defaut constructor :** A constructor with 0 parameters is known as default constructor.
18+
- Syntax :
19+
20+
```
21+
<class_name>(){
22+
//code to be executed on the execution of the constructor
23+
}
24+
```
25+
- Example :
26+
27+
```
28+
class CWH {
29+
CWH(){
30+
System.out.println("This is the default constructor of CWH class.");
31+
}
32+
}
33+
public class CWH_constructors {
34+
public static void main(String[] args) {
35+
CWH obj1 = new CWH();
36+
}
37+
}
38+
```
39+
40+
```
41+
Output :
42+
This is the default constructor of CWH class.
43+
```
44+
45+
- In the above code, CWH() is the constructor of class CWH The CWH() constructor is invoked automatically with the creation of object ob1.
46+
47+
**2. Paramerterized constructor :** A constructor with some specified number of parameters is known as a parameterized constructor.
48+
- Syntax :
49+
50+
```
51+
<class-name>(<data-type> param1, <data-type> param2,......){
52+
//code to be executed on the invocation of the constructor
53+
}
54+
```
55+
56+
- Example :
57+
58+
```
59+
class CWH {
60+
CWH(String s, int b){
61+
62+
System.out.println("This is the " +b+ "th video of "+ " "+ s);
63+
}
64+
}
65+
public class CWH_constructors {
66+
public static void main(String[] args) {
67+
CWH obj1 = new CWH("CodeWithHarry Java Playlist",42);
68+
}
69+
}
70+
```
71+
72+
```
73+
Output :
74+
This is the 42th video of CodeWithHarry Java Playlist
75+
````
76+
77+
- In the above example, CWH() constructor accepts two parameters i.e., string s and int b.
78+
79+
### Constructor Overloading in Java :
80+
- Just like methods, constructors can also be overloaded in Java. We can overload the Employe constructor like below:
81+
82+
```
83+
public Employee (String n)
84+
name = n;
85+
}
86+
```
87+
88+
**Note:**
89+
1. Constructors can take parameters without being overloaded
90+
2. There can be more than two overloaded constructors
91+
92+
- Let's take an example to understand the concept of constructor overloading.
93+
94+
**Example :**
95+
96+
- In the below example, the class Employee has a constructor named Employee(). It takes two argument,i.e., string s & int i. The same constructor is overloaded and then it accepts three arguments i.e., string s, int i & int salary.
97+
98+
```
99+
class Employee {
100+
// First constructor
101+
Employee(String s, int i){
102+
System.out.println("The name of the first employee is : " + s);
103+
System.out.println("The id of the first employee is : " + i);
104+
}
105+
// Constructor overloaded
106+
Employee(String s, int i, int salary){
107+
System.out.println("The name of the second employee is : " + s);
108+
System.out.println("The id of the second employee is : " + i);
109+
System.out.println("The salary of second employee is : " + salary);
110+
}
111+
112+
}
113+
public class CWH_constructors {
114+
public static void main(String[] args) {
115+
Employee shubham = new Employee("Shubham",1);
116+
Employee harry = new Employee("Harry",2,70000);
117+
118+
119+
120+
}
121+
}
122+
```
123+
124+
```
125+
Output :
126+
127+
The name of the first employee is : Shubham
128+
The id of the first employee is : 1
129+
The name of the second employee is : Harry
130+
The id of the second employee is : 2
131+
The salary of second employee is : 70000
132+
```
133+
134+
**Quick quiz:** Overloaded the employee constructor to initialize the salary to Rs 10,000
135+
136+
```
137+
class Employee {
138+
// First constructor
139+
Employee(String s, int i){
140+
System.out.println("The name of the first employee is : " + s);
141+
System.out.println("The id of the first employee is : " + i);
142+
}
143+
// Constructor overloaded
144+
Employee(String s, int i, int salary){
145+
System.out.println("The name of the second employee is : " + s);
146+
System.out.println("The id of the second employee is : " + i);
147+
System.out.println("The salary of second employee is : " + salary);
148+
}
149+
// Overloaded constructor with salary 10,000
150+
Employee(int salary){
151+
System.out.println("The salary of third employee is : " + salary);
152+
}
153+
154+
}
155+
public class quick_quiz {
156+
public static void main(String[] args) {
157+
Employee shubham = new Employee("Shubham",1);
158+
Employee harry = new Employee("Harry",2,70000);
159+
Employee kishan = new Employee(10000);
160+
}
161+
}
162+
```
163+
164+
### Code as described in the video:
165+
166+
```
167+
class MyMainEmployee{
168+
private int id;
169+
private String name;
170+
171+
public MyMainEmployee(){
172+
id = 0;
173+
name = "Your-Name-Here";
174+
}
175+
public MyMainEmployee(String myName, int myId){
176+
id = myId;
177+
name = myName;
178+
}
179+
public MyMainEmployee(String myName){
180+
id = 1;
181+
name = myName;
182+
}
183+
public String getName(){
184+
return name;
185+
}
186+
public void setName(String n){
187+
this.name = n;
188+
}
189+
public void setId(int i){
190+
this.id = i;
191+
}
192+
public int getId(){
193+
return id;
194+
}
195+
}
196+
197+
public class cwh_42_constructors {
198+
public static void main(String[] args) {
199+
//MyMainEmployee harry = new MyMainEmployee("ProgrammingWithHarry", 12);
200+
MyMainEmployee harry = new MyMainEmployee();
201+
//harry.setName("CodeWithHarry");
202+
//harry.setId(34);
203+
System.out.println(harry.getId());
204+
System.out.println(harry.getName());
205+
}
206+
}
207+
```
208+
209+
**Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-42/Chapter9.pdf)**
210+
211+
**Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-42/UltimateJavaCheatSheet.pdf)**

0 commit comments

Comments
 (0)