0% found this document useful (0 votes)
2 views3 pages

Encapsulation Employee Assignment

Encapsulation in Java is an Object-Oriented Programming concept that combines data and methods into a class, protecting data by making variables private and using public getter and setter methods. An example is provided with an Employee class that demonstrates how to implement encapsulation and manage employee data using arrays. The conclusion emphasizes that encapsulation enhances data security and maintainability of programs.

Uploaded by

shalinsunil1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Encapsulation Employee Assignment

Encapsulation in Java is an Object-Oriented Programming concept that combines data and methods into a class, protecting data by making variables private and using public getter and setter methods. An example is provided with an Employee class that demonstrates how to implement encapsulation and manage employee data using arrays. The conclusion emphasizes that encapsulation enhances data security and maintainability of programs.

Uploaded by

shalinsunil1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ENCAPSULATION IN JAVA - ASSIGNMENT

What is Encapsulation?

Encapsulation is an Object-Oriented Programming


concept where data (variables) and methods are
combined into a single unit called a class. It protects the
data by making variables private and providing public
getter and setter methods to access them. This
improves security, control, and maintainability of the
program.
How to Implement Encapsulation?

1. Declare variables as private


2. Provide public getter and setter methods
3. Access data only through these methods
Create Employee Class (Encapsulation Example)
class Employee {
private int id;
private String name;
private int age;
private double salary;
private String dept;

public Employee(int id, String name, int age, double salary, String dept) {
[Link] = id;
[Link] = name;
[Link] = age;
[Link] = salary;
[Link] = dept;
}
public int getId() { return id; }
public String getName() { return name; }
public int getAge() { return age; }
public double getSalary() { return salary; }
public String getDept() { return dept; }

public void setSalary(double salary) {


[Link] = salary;
}
}
Store Employees in Array and Process Salaries
public class Main {
public static void main(String[] args) {

Employee[] emp = new Employee[3];

emp[0] = new Employee(1, "Shalin", 22, 25000, "IT");


emp[1] = new Employee(2, "Arun", 25, 30000, "HR");
emp[2] = new Employee(3, "Kiran", 24, 28000, "Finance");

double total = 0;

for(Employee e : emp) {
total += [Link]();
}

double avg = total / [Link];

[Link]("Average Salary: " + avg);

for(Employee e : emp) {
if([Link]() > avg) {
double newSalary = [Link]() + 2000; // hike
[Link](newSalary);
}
}
for(Employee e : emp) {
[Link]([Link]() + " " + [Link]() + " " + [Link]());
}
}
}

Conclusion

Encapsulation protects data and provides controlled


access through methods. It improves data security and
makes the program more reliable and easy to maintain.
Using arrays and loops, we can manage multiple
employee objects efficiently.

You might also like