Encapsulation in Java programming language

Encapsulation in Java programming language

In this tutorial, we will discuss the concept of the Encapsulation in Java programming language

Encapsulation is one of the most important concepts of Oop programs. Other some Oop concepts are inheritance, polymorphism and abstraction

Basically, It is used for security purposes in Java. We can use it in ATM, password protection…

Encapsulation means binding data members and methods into a single unit with data security. It helps to hide the implementation details from the user. If the data members are private, it can only be accessed from the same class.

No one outside the class can access private data members(variable) of other classes. It is known as data-hiding in java. Encapsulation of the variables of ` class will be hidden from other classes.

However, we can setup a getter and setter method.  We can make the class read-only or write-only.

To use encapsulation in Java, you must declare the data members of a class as private(for security).


Advantages of Encapsulation

It improves maintainability and flexibility and re-useability.

The user would not know what is going on behind the scene. They would only be knowing that to update a field call set method and to read a field call get method but what these set and get method are doing is purely hidden from them

How does encapsulation work in Java

Encapsulation works in Java with the following reasons

Declaring the properties or variables as private – Encapsulation can be achieved by Declaring all the properties or variables as private. these variables can be accessed by public methods of the class

 

For example, we are creating a class student. the variable must be declared as private

 

private string stu_Name

private int stu_Id;

provate int stu_Marks;

 

Create public methods inside the class in order to get and set(getter and setter) the attributes or variables

public string get_stu_Name(){
return stu_Name;
}


public int get_stu_Id(){
return stu_Id;
}

public int get_stu_Marks(){
return stu_Marks;
}

public void set_stu_Name(string stu_Name){
this.stu_Name=stu_Name;
}


public void set_stu_Id(int stu_Id){
this.stu_Id =stu_Id;
}

public void set_stu_Marks(int stu_Marks){
this.stu_Marks=stu_Marks;
}

 

Program 1

When the above code is executed it produces the following result
At the above program, has two classes. once StudentName other CheckName.
An object is created at CheckName class for StudentName class.
Declare a variable f_Name assigned as String(data type) in StudentName class
Created setter and getter methods in  StudentName class.
Finally called s.getf_Name(); and display this  output

 

Program 2

Example
Explanation of above the program
Example

Program 3

Example
Example

When the above code is executed it produces the following result

Output

Related Links


Abstraction in C++                                          Abstract class in Java

Encapsulation in C++                                    

Polymorphism in C++                                      Methodoverriding in Java

Method overloading in java                             Constructor overloading in Java

Exception Handling in Java

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.