Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Encapsulation in Java programming language

Posted on February 10, 2017January 29, 2020

Table of Contents

  • Encapsulation in Java programming language
  • Related Links

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

Encapsulation in Java
Example
Explanation of above the program
Encapsulation in Java
Example

Program 3

Encapsulation in Java
Example
Encapsulation in Java
Example

When the above code is executed it produces the following result

Encapsulation in Java
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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes