0% found this document useful (0 votes)
4 views

Java Encapsulation Interview Questions

The document discusses Java encapsulation, defining it as the process of wrapping data and methods into a single unit by using private class variables and public getter and setter methods. It highlights the benefits of encapsulation, including improved code maintainability, data control, and security. Additionally, it provides examples and contrasts encapsulation with abstraction.

Uploaded by

mullaabuzar61
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Encapsulation Interview Questions

The document discusses Java encapsulation, defining it as the process of wrapping data and methods into a single unit by using private class variables and public getter and setter methods. It highlights the benefits of encapsulation, including improved code maintainability, data control, and security. Additionally, it provides examples and contrasts encapsulation with abstraction.

Uploaded by

mullaabuzar61
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Encapsulation Interview Questions with Answers and Codes

1. What is encapsulation in Java?

Encapsulation is the process of wrapping data (fields) and methods into a single unit. In Java,

encapsulation is achieved by declaring class variables as private and providing public getter and

setter methods to access and modify them.

2. What are the benefits of encapsulation?

- Improves code maintainability and flexibility.

- Provides control over data by restricting direct access.

- Ensures better security by hiding sensitive information.

- Helps achieve modularity and maintainability.

3. How can encapsulation be implemented in Java?

Encapsulation can be implemented by:

1. Declaring the fields of a class as private.

2. Providing public getter and setter methods to access and update the private fields.

Example:

class Student {

private String name;

public String getName() {

return name;

}
public void setName(String name) {

this.name = name;

4. How does encapsulation improve code maintainability?

Encapsulation allows changes to the implementation of a class without affecting the classes that use

it. By using getter and setter methods, you can modify the internal logic without changing the

external interface.

5. Explain the difference between encapsulation and abstraction.

- Encapsulation focuses on binding data and methods together and controlling access to data.

- Abstraction focuses on hiding implementation details and showing only the functionality to the

user.

6. Write a program to demonstrate encapsulation.

Example:

class Employee {

private int empId;

private String empName;

public int getEmpId() {

return empId;

public void setEmpId(int empId) {

this.empId = empId;
}

public String getEmpName() {

return empName;

public void setEmpName(String empName) {

this.empName = empName;

class Main {

public static void main(String[] args) {

Employee emp = new Employee();

emp.setEmpId(101);

emp.setEmpName("John Doe");

System.out.println("Employee ID: " + emp.getEmpId());

System.out.println("Employee Name: " + emp.getEmpName());

You might also like