0% found this document useful (0 votes)
3 views2 pages

Encapsulation

Encapsulation is a programming concept that involves hiding sensitive data by declaring class variables as private and providing public get and set methods for access. This allows controlled access to private variables while enhancing data security and flexibility in code management. The document includes an example in Java demonstrating how to implement encapsulation with a class that manages private attributes and their access methods.
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)
3 views2 pages

Encapsulation

Encapsulation is a programming concept that involves hiding sensitive data by declaring class variables as private and providing public get and set methods for access. This allows controlled access to private variables while enhancing data security and flexibility in code management. The document includes an example in Java demonstrating how to implement encapsulation with a class that manages private attributes and their access methods.
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
You are on page 1/ 2

Encapsulation

The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from
users. 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:

• You learned from the previous chapter that 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.

Example:

import java.util.*;
public class Encapsulation {
private String name = "Subhankar";
private String mob = "7554565514";
void defData() {
System.out.println("The name is: " + name);
System.out.println("The number is: " + mob);
}
String getAccess() {
return this.mob;
}
void setAccess(String number) {
this.mob = number;
}
}
class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String num;
Encapsulation en = new Encapsulation();
en.defData();
do {
System.out.print("Enter number for update: ");
num = sc.next();
if (num.length() == 10) {
en.setAccess(num);
break;
} else
System.out.println("Please enter correctly !\n");
} while (true);
en.defData();
}
}

Output:

Why Encapsulation?

• Better control of class attributes and methods


• Class attributes can be made read-only (if you only use the get method), or write-
only (if you only use the set method)
• Flexible: the programmer can change one part of the code without affecting other
parts
• Increased security of data

You might also like