In this tutorial, we will discuss Encapsulation in Java programming language.
Encapsulation is one of the most important OOP concepts in Java similar C++ and Python.
Other Oop concepts are inheritance, Polymorphism and Abstraction
Encapsulation is used for security purpose. It binds data members and methods into a single unit with data security. Encapsulation helps to hide the implementation details from the user. This is because data values are private.
Private data member (Variable ) cannot be accessed from outside of class. It is known as data hiding in encapsulation.
To use encapsulation in Java, data member in the class must be declared as private(for security.
It improves maintainability, flexibility, and re-usability
Users would not know about what is going on behind on the scenes:
Encapsulation makes data hiding in Java because other class will not be able to access the private data members inside the class
The following shows a simple example of encapsulation of Java:
Program 1
class stu_name{ private String name; //global variable declaration as private //this variable access only public methods of class void set_name(String name) {//set method with argument this.name=name;//this method can access private variale //assign local variable to global variable } String get_name(){//get method return name;//this method can access private variale } } class stu_det1{ public static void main(String args[]){ stu_name st=new stu_name(); //create object for class stu_name st.set_name("kannan"); //call set method System.out.println(st.get_name()); }//call get method }
//save this file as stu_det1.java
When the above program is compiled and executed, it produces the following result
kannan
In the above program, we demonstrate about encapsulation in Java. The variable set as private. The get method(get_Name()) and set method (set_Name())set as public. These two methods are used to access these variables(name) for arguments.
Program 2
class time{ private int hour; //the global variable declared as private private int minute; private int second; // hour, minute, second are private variables public void displayTime() // method is declared for calculate time { System.out.println("The time is:"+hour+":"+minute+":"+second); } public void setHour(int h){ //set method for hour to access hour=h; } public int getHour(){ //get method for hour to access return hour; } public void setMinute(int m){ //set method for minute to access minute=m; } public int getMinute(){ //get method for second to access return minute; } public void setSecond(int s){ //set method for second to access second=s; } public int getSecond(){ //get method for second to access return second; } } public class clock{ public static void main(String args[]){ time t=new time(); //setting values of the variables t.setHour(3); t.setMinute(05); t.setSecond(45); //call method to calculate display time t.displayTime(); } }
When the above program is compiled and executed, it produces the following result.
The time is:3:5:45
Program 3
class studentdetails{ //this class save as studentdetails.java String name; int id; int m1; int m2; int m3; int tot; int avg; void set_Name(String name,int id){ this.name=name; this.id=id; } void get_Name(){ System.out.println("student name is:"+name); System.out.println("student id is:"+id); } void set_Marks(int m1,int marks2,int marks3){ this.m1=m1; m2=marks2; m3=marks3; } void get_Marks(){ System.out.println("student marks1 is:"+m1); System.out.println("student marks2 is:"+m2); System.out.println("student marks3 is:"+m3); System.out.println("student total is:"+tot); System.out.println("student average is:"+avg); System.out.println("......."); } void calculate() { tot=m1+m2+m3; avg=tot/3; } }
class calctot{//this class save as clactot.java public static void main(String args[]){ studentdetails st = new studentdetails(); st.set_Name("Kanthavel",34); st.set_Marks(67,78,89); st.get_Name(); st.calculate(); st.get_Marks(); } }
When the above program is compiled and executed, it produces the following result:
student name is:kanthavel student id is:34 student marks1 is:67 student marks2 is:78 student marks3 is:89 student total is:234 student average:78 .............
Suggested for you
Encapsulation in Java language
Abstract class in Java languag
Method overloading in Java language
Method overriding in Java language
Constructor overloading in Java language
difference to constructor and method in Java
Encapsulation in Java language
How to set a path to Java language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…