4. 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
object.
• In encapsulation, the variables of a class will be hidden from other classes, and can be
accessed only through the methods of their current class. Therefore, it is also known as data
hiding.
4
5. To achieve this, you must:
1. declare class variables/attributes as private
2. provide public get and set methods to access and update the value of a
private variable
How to implement encapsulation
5
6. • 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 studntID, then the methods names will be
getStudentId() and setStudentId().
Get and Set Methods
6
7. 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
7
8. • The set method takes a parameter (newName) and assigns it to the name variable. The
‘this’ keyword is used to refer to the current object.
• The get method returns the value of the name variable.
• However, as the name variable is declared as private, we cannot access it from outside this
class
Example - 1 explained
8
9. 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;
}
}
Example – 1 (Error)
9
public class Main
{
public static void main(String[] args)
{
Person myObj = new Person();
myObj.name = "John"; // error
System.out.println(myObj.name); // error
}
}
10. We can access the private variables with setter and getter methods of that calss.
public class Person {
private String name; // private = restricted access
// Setter
public void setName (String newName) {
this.name = newName;
}
// Getter
public String getName() {
return name;
}
}
Example – 1
10
public class Main
{
public static void main(String[] args)
{
Person myObj = new Person();
myObj.setName("John");
// Set the value of the name variable
to “John"
System.out.println(myObj.getName());
}
}
// Outputs "John"
11. 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;
}
}
Example – 2
11
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
12. UML representation of Example – 2
12
Person
- name: String
- age: int
+Person(String, int)
+setName(String) : void
+setId(int) : void
+getName(): String
+getId():int
MyClass
+main(String [] ) : void
13. 13
Check Balance
Update Balance
ATM GUI
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 procedure. Being the end
user we just need to knowhow to use the card.
[Encapsulation = Data Hiding + Abstraction]
14. public class Account {
private double balance; // data hiding
public double getBalance () {
//Validation
return balance;
}
public void setBalance(double balance) {
//Validation
this.balance = balance;
}
}
Example – 2
[Encapsulation = Data Hiding + Abstraction]
14
Check Balance
Update Balance
ATM GUI
18. • 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 requirement.
• Reusability: Encapsulation also improves the re-usability and is easy to change with new
requirements. 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 testing.
Advantage of Encapsulation
18