Make sure to subscribe to our newsletter and be the first to know the news.
Make sure to subscribe to our newsletter and be the first to know the news.
A class serves as a blueprint for creating multiple objects with similar characteristics and functionalities. It encapsulates data and methods within a single unit, promoting code reusability, modularity, and maintainability.
Understanding the Code
Attributes:
· The class has three attributes: "name," "gender," and "age," representing the name, gender, and age of an employee, respectively.
Methods:
1. setName(String name): Sets the name of the employee.
2. setGender(String gender): Sets the gender of the employee.
3. setAge(int age): Sets the age of the employee.
4. getName(): Retrieves the name of the employee.
5. getGender(): Retrieves the gender of the employee.
6. getAge(): Retrieves the age of the employee.
7. displayDetails(): Displays the details of the employee, including name, gender, and age.
The class follows encapsulation principles by providing setter and getter methods to manipulate and access the attributes while hiding the internal implementation details.
public class Employee {
String name,gender;
int age;
public void setName(String name){
this.name=name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName(){
return name;
}
public void displayDetails(){
System.out.println("Employee Name:"+name);
System.out.println("Employee Gender:"+gender);
System.out.println("Employee Age:"+age);
}
}
public class EmployeeRunner {
public static void main(String[] args) {
Employee e=new Employee();
e.setName("Coding Owl");
e.setAge(25);
e.setGender("Male");
e.displayDetails();
}
}
Conclusion:
The "Employee" class presented in the code encapsulates the attributes and behaviors of an employee in an object-oriented manner. By defining the class, developers can create multiple instances of "Employee" objects, each with its own set of attributes and methods. Encapsulation