Encapsulation in Java
Contents
- Encapsulation in Java
- How to implement encapsulation
- Advantage of Encapsulation
Introduction to Encapsulation
- Encapsulation is one of the four fundamental OOP concepts.
- The other three are inheritance, polymorphism, and abstraction.
- It is the process of binding data and the corresponding methods under a single unit.
- It is the process of hiding information details and protecting data and behavior of the
- In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current Therefore, it is also known as data hiding.
How to implement encapsulation
To achieve this, you must:
- declare class variables/attributes as private
- provide public get and set methods to access and update the value of a private variable
Get and Set Methods
- We know, private variables can only be accessed within the same class (an outside class has no access to it). However, it is possible to access them if we provide public get and set methods.
- The get method returns the variable value, and the set method sets the value.
- Syntax for both is that they start with either get or set, followed by the name of the variable, with the first letter in upper case:
e.g. If the name of the variable is student_ID, then the methods names will be
getStudentId() and setStudentId().
Example – 1
public class Person {
private String name; // private = restricted access
// Setter Method
public void setName (String newName) { this.name = newName;
}
// Getter Method
public String getName() { return name;
}
}
Example – 1 explained
- The set method takes a parameter (newName) and assigns it to the name variable. The ‘this’ keyword is used to refer to the current
- The get method returns the value of the name
- However, as the name variable is declared as private, we cannot access it from outside this class
Example – 1 (Error) | ||
As the name variable is declared as private, we cannot access it from outside this class
public class Person { |
||
private String name; // private = restricted access
// Setter public void setName (String newName) { this.name = newName; } // Getter public String getName() { return name; } |
public class Main
{ public static void main(String[] args { Person myObj = new Person(); myObj.name = “John”; // error System.out.println(myObj.name); // er } } |
)
ror |
} | ||
|
Example – 2 | ||
public class Person { private String name; | ||
private int age;
public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public int getAge() { return age; } } |
public class EncapsulaionExample
{ public static void main(String[] args) { Person p1 = new Person(); p1.setName(“Asha”); p1.setAge(20); System.out.println(“Name: “+p1.getName()) System.out.println(“Age: “+p1.getAge( } } |
; )); |
// Outputs Name: Asha Age: 20 | ||
[Encapsulation = Data Hiding + Abstraction]
Data Hiding: By declaring variables as private, we can achieve data hiding. we cannot access it from outside this class
Abstraction: Highlighting the service that are offering without explaining internal design/information is the concept of Abstraction.
Using ATM Card,
- We just swipe the card
- Enter the password
- Do the transaction
- We don’t know the internal Being the end user we just need to knowhow to use the card.
ATM GUI
Check Balance
Update Balance |
Example – 2
[Encapsulation = Data Hiding + Abstraction]
public class Account {
private double balance; // data hiding
public double getBalance () {
//Validation return balance;
ATM GUI
}
public void setBalance(double balance) {
//Validation this.balance = balance;
}
}
Advantage of Encapsulation
- Data Hiding: It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members. It increases the security of data
- Increased Flexibility: By providing only a setter or getter method, you can make the class read-only or write-only depending on our
- Reusability: Encapsulation also improves the re-usability and is easy to change with new The programmer can change one part of the code without affecting other parts.
- Testing code is easy: Encapsulated code is easy to test for unit
Thank you!