-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployee.java
More file actions
37 lines (33 loc) · 1.19 KB
/
Copy pathEmployee.java
File metadata and controls
37 lines (33 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class Employee {
private int id;
private String name;
// Default constructor
Employee() {
System.out.println("Employee Created");
this.id = 999;
this.name = "John Doe";
System.out.println("Default Employee Created");
System.out.println("ID = " + id + ", Name = " + name);
}
// Constructor with id only
Employee(int id) {
System.out.println("Employee Created");
this.id = id;
this.name = "John Doe"; // default name
System.out.println("Employee Created with Default Name");
System.out.println("ID = " + id + ", Name = " + name);
}
// Constructor with name and id
Employee(String name, int id) {
System.out.println("Employee Created");
this.id = id;
this.name = name;
System.out.println("ID = " + id + ", Name = " + name);
}
public static void main(String[] args) {
Employee emp = new Employee(); // Calls default constructor
Employee emp1 = new Employee(10); // Calls constructor with id
Employee emp2 = new Employee("Pankaj", 20); // Calls constructor with name, id
System.out.println(emp);
}
}