Encapsulation
In object-oriented design, encapsulation is the concept of hiding the internal state of an object and providing public methods to access and modify that state. It allows us to control the access to the data and ensure that it is used in a consistent and valid way.
Encapsulation is important for several reasons:
- Data Protection: By encapsulating the data within an object, we can protect it from being accessed or modified directly by external code. This helps maintain the integrity and consistency of the data.
- Modularity: Encapsulation allows us to divide our code into smaller modules, making it easier to read, understand, and maintain. Each object encapsulates its own data and behaviors, making it self-contained and reusable in different contexts.
- Flexibility: Encapsulated objects can have hidden implementation details. This means that we can change the internal implementation without affecting the code that uses the object. This provides flexibility and reduces the impact of changes.
Let's take an example to understand encapsulation in Java:
1public class Person {
2
3 private String name;
4 private int age;
5
6 public Person(String name, int age) {
7 this.name = name;
8 this.age = age;
9 }
10
11 public String getName() {
12 return name;
13 }
14
15 public void setName(String name) {
16 this.name = name;
17 }
18
19 public int getAge() {
20 return age;
21 }
22
23 public void setAge(int age) {
24 this.age = age;
25 }
26
27 public void printInformation() {
28 System.out.println("Name: " + name);
29 System.out.println("Age: " + age);
30 }
31
32 public static void main(String[] args) {
33 Person person = new Person("John", 25);
34 person.printInformation();
35
36 person.setName("Jane");
37 person.setAge(30);
38 person.printInformation();
39 }
40}
In this example, we have a Person
class with private instance variables name
and age
. These variables are encapsulated within the class, meaning that they cannot be accessed directly from outside the class.
To access and modify the name
and age
, we provide getter and setter methods. The getter methods (getName()
and getAge()
) allow other code to retrieve the values, while the setter methods (setName()
and setAge()
) provide a way to update the values.
The printInformation()
method is a public method that can be used to print the details of the person. It accesses the encapsulated data through the getter methods.
By encapsulating the data and providing controlled access through getter and setter methods, we ensure that the person's information is accessed and modified in a controlled and consistent manner.
Encapsulation is a fundamental principle of object-oriented design and plays a crucial role in creating well-designed, modular, and maintainable code.
xxxxxxxxxx
}
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void printInformation() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);