Skip to content

Commit 320c9ca

Browse files
Constructors in Java
1 parent cca16f1 commit 320c9ca

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

42.Constructors/CWH_constructors.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Employee {
2+
// First constructor
3+
Employee(String s, int i){
4+
System.out.println("The name of the first employee is : " + s);
5+
System.out.println("The id of the first employee is : " + i);
6+
}
7+
// Constructor overloaded
8+
Employee(String s, int i, int salary){
9+
System.out.println("The name of the second employee is : " + s);
10+
System.out.println("The id of the second employee is : " + i);
11+
System.out.println("The salary of second employee is : " + salary);
12+
}
13+
14+
}
15+
public class CWH_constructors {
16+
public static void main(String[] args) {
17+
Employee shubham = new Employee("Shubham",1);
18+
Employee harry = new Employee("Harry",2,70000);
19+
20+
}
21+
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class MyMainEmployee{
2+
private int id;
3+
private String name;
4+
5+
public MyMainEmployee(){
6+
id = 0;
7+
name = "Your-Name-Here";
8+
}
9+
public MyMainEmployee(String myName, int myId){
10+
id = myId;
11+
name = myName;
12+
}
13+
public MyMainEmployee(String myName){
14+
id = 1;
15+
name = myName;
16+
}
17+
public String getName(){
18+
return name;
19+
}
20+
public void setName(String n){
21+
this.name = n;
22+
}
23+
public void setId(int i){
24+
this.id = i;
25+
}
26+
public int getId(){
27+
return id;
28+
}
29+
}
30+
31+
public class cwh_42_constructors {
32+
public static void main(String[] args) {
33+
//MyMainEmployee harry = new MyMainEmployee("ProgrammingWithHarry", 12);
34+
MyMainEmployee harry = new MyMainEmployee();
35+
//harry.setName("CodeWithHarry");
36+
//harry.setId(34);
37+
System.out.println(harry.getId());
38+
System.out.println(harry.getName());
39+
}
40+
}

42.Constructors/quick_quiz.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Employee {
2+
// First constructor
3+
Employee(String s, int i){
4+
System.out.println("The name of the first employee is : " + s);
5+
System.out.println("The id of the first employee is : " + i);
6+
}
7+
// Constructor overloaded
8+
Employee(String s, int i, int salary){
9+
System.out.println("The name of the second employee is : " + s);
10+
System.out.println("The id of the second employee is : " + i);
11+
System.out.println("The salary of second employee is : " + salary);
12+
}
13+
// Overloaded constructor with salary 10,000
14+
Employee(int salary){
15+
System.out.println("The salary of third employee is : " + salary);
16+
}
17+
18+
}
19+
public class quick_quiz {
20+
public static void main(String[] args) {
21+
Employee shubham = new Employee("Shubham",1);
22+
Employee harry = new Employee("Harry",2,70000);
23+
Employee kishan = new Employee(10000);
24+
}
25+
}

0 commit comments

Comments
 (0)