Updated April 5, 2023
Introduction to Encapsulation in OOPs
- Encapsulation is an essential part of the object-oriented programming language to operate data.
- It helps to combine code and data in one unit and protect the coding data.
- It is an important function in OOP language to hide coding data and object behaviors.
- It is a basic concept of the object-oriented programming language to hide important methods, variables, and coding procedures for safety and security.
- It is also a mechanism to wrap or hide variables, methods, and objects.
- The object-oriented programming languages include part to safety and privacy of the data using method.
- The wrapping data and operating methods are in one container and shown as per the user’s requirement.
Syntax:
- It requires a private access modifier for the variable.
- To OOPs programming language may access private variable or object using the method.
- The getter and setter method uses to access and display data.
The following syntax is of a private variable.
private data_type variable_name;
Example: private int numbers;
- Syntax of getter method.
public data_type getMethodName(){
return variable_name;
}
Example:
public int getNumbers(){
return numbers;
}
- Syntax of setter method.
public void setMethodName(){
return variable_name;
}
Example:
public void setNumbers (int newNumbers){
numbers = newNumbers;
}
- The access variable data in syntax is below.
Object.setMethod("data");
Example:
object_name.setNumbers(123221);
- The retrieved data from the encapsulation method is below.
Object.getMethod()
Example:
object_name.getNumbers()
How does Encapsulation work in OOPs?
- It works on every object-oriented programming language.
- You can use C, C#, PHP, java, and python language.
- Here, you use the Java programming language .
- Create a class in a java file.
Public class Encapsulationoop{
Write code here…
}
- Create a private variable for encapsulation.
private int numbers;
- Use the getter and setter method for the respective variable.
public int getNumbers(){
return numbers;
}
public void setNumbers (int newNumbers){
numbers = newNumbers;
}
- Create the main method in the java class.
public static void main(String []args){
Write code here…
}
- Create an object of the class in the main method.
Encapsulationoop encap = new Encapsulationoop();
- Access the private data.
encap.setNumbers(123221);
- Retrieve encapsulated data using the print function.
System.out.println(encap.getNumbers());
- Combine steps of the working procedure of the encapsulation.
Public class Encapsulationoop{
private int numbers;
public int getNumbers(){
return numbers;
}
public void setNumbers (int newNumbers){
numbers = newNumbers;
}
public static void main(String []args){
Encapsulationoop encap = new Encapsulationoop();
encap.setNumbers(123221);
System.out.println(encap.getNumbers());
}}
Examples
Here are the following examples mention below
Example #1
The basic example and output below.
Code:
public class Encapsulationoop{
private int numbers;
public int getNumbers(){
return numbers;
}
public void setNumbers (int newNumbers){
numbers = newNumbers;
}
public static void main (String [] args){
Encapsulationoop encap = new Encapsulationoop();
System.out.println("Encapsulation in OOPs");
encap.setNumbers (123221);
System.out.println ("number:" +encap.getNumbers());
}}
Output:
Example #2
Java example and output below.
Code:
public class Encapsulationoop{
private int numbers;
private String names;
private double points;
public int getNumbers(){
return numbers;
}
public void setNumbers (int newNumbers){
numbers = newNumbers;
}
public String getNames(){
return names;
}
public void setNames (String newNames){
names = newNames;
}
public double getPoints(){
return points;
}
public void setPoints (double newPoints){
points = newPoints;
}
public static void main (String []args){
Encapsulationoop encap = new Encapsulationoop();
encap.setNumbers (123221);
System.out.println ("number:" +encap.getNumbers());
encap.setNames ( "encapsualtion in OOPs");
System.out.println ("string:" +encap.getNames ());
encap.setPoints (23.12);
System.out.println ("float:" +encap.getPoints());
}}
Description
- The above example shows encapsulation in a java programming language.
- The getter and setter method uses for the protection of the data and variables.
- The get and set keyword helps to access and retrieve an object, variable, and method.
Output
Example #3
PHP example and output below.
Code:
<!DOCTYPE>
<html>
<body>
<?php
class Encapsulationoop {
private $numbers;
private $names;
private $points;
public function getNumbers() {
return $this -> numbers;
}
public function setNumbers($numbers) {
$this->numbers = $numbers;
echo("numbers: ".$numbers);
echo("<br>");
}
public function getNames() {
return $this -> names;
}
public function setNames($names) {
$this -> names = $names;
echo("names: ".$names);
echo("<br>");
}
public function getPoints() {
return $this -> points;
}
public function setPoints($points) {
$this -> points = $points;
echo("points: ".$points);
echo("<br>");
}
}
$ecap = new Encapsulationoop();
$ecap -> setNumbers(12343221);
$ecap -> setNames("encapsulation in OOPs");
$ecap -> setPoints(23.41);
?>
</body>
</html>
Description
- The above example shows in the PHP programming language.
- The getter and setter method uses for the protection of the data and variables.
- The get and set keyword helps to access and retrieve an object, variable, and method.
- The PHP does not need to assign a particular data type for a variable.
Output:
Example #4
C++ example and output below.
Code:
#include <iostream>
using namespace std;
class Encapsulationoop
{
private:
int numbers;
int marks;
public:
void set(int number)
{
numbers = number;
}
int get()
{
return numbers;
}
};
int main()
{
Encapsulationoop ecap;
ecap.set(123221);
cout << "encpsulation in oop \n";
cout << ecap.get();
return 0;
}
Description
- The above example shows encapsulation in the C++ programming language.
- The getter and setter method uses for the protection of the data and variables.
- The get and set keyword helps to access and retrieve an object, variable, and method.
Output:
Example #5
The encapsulation in python example and output below.
Code:
class Encap:
def __init__(self):
self._first = 12453221
class Dev(Encap):
def __init__(self):
Encap.__init__(self)
print(" Encapsulation in OOPs ")
print(self._first)
encap1 = Dev()
encap2 = Encap()
print(encap2.first)
Output:
Description
- The above example shows encapsulation in a python programming language.
- The base and derived classes help to protect the object, variable, and method.
- The “_first” variable helps to protect and access data in the program.
Conclusion
- It is a function of hiding data, methods, and variables for safety.
- The encapsulation is structure to wrap different methods, objects, and variables in a single wrapping unit.
- It helps to protect data from unwanted users and displays required data.
- It creates a protected, safe, and user-friendly web application for every user.
Recommended Articles
We hope that this EDUCBA information on “Encapsulation in OOPs” was beneficial to you. You can view EDUCBA’s recommended articles for more information.