Here is the Employee class which is used in many of the examples in this site. Employee class has four properties namely id, name, department and age and has getters and setters methods for these properties. Default constructor has also been overloaded to accept id, name and department properties and second overloaded constructor accepts all four properties of an employee.
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
public class Employee{ private String id; private String name; private String department; private int age; public Employee(){ } public Employee(String id, String name, String department){ this.id = id; this.name = name; this.department = department; } //constructor with age argument public Employee(String id, String name, String department, int age){ this.id = id; this.name = name; this.department = department; this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString(){ return "[" + this.getId() + " : " + this.getName() + " : " + this.getDepartment() + "]"; } } |
Employee class overrides the toString
method so when Employee object is printed directly using the System.out.println
statement, it produces nice summary.
This class has been referred by many examples for demonstration purpose.
Excellent tutorials, the questions in the quiz’s test understanding not memory.
Excellent 5 star
Hello George,
Happy to know that you liked it. Please like our facebook page to get notified of new examples.
Thanks.
Thanks Rahim