Encapsulation in Java - Study24x7
Social learning Network
study24x7

Default error msg

Login

New to Study24x7 ? Join Now
Already have an account? Login

Encapsulation in Java

Updated on 15 February 2022
study24x7
Java Object oriented programmi
7 min read 4 views
Updated on 15 February 2022

There are four fundamentals of object-oriented programming. Encapsulation is one of the four and important features of object-oriented programming. 



To achieve encapsulation in java




Example: 


package Java_Test; 

class Encap{ 

//Private Variables father_mobile_no and stu_email_id private long father_mobile_no;

private String stu_email_id; 

public String getStu_email_id() { 

return stu_email_id;

}

public void setStu_email_id(String stu_email_id) { 

this.stu_email_id = stu_email_id;

} public long getFather_mobile_no() { 

return father_mobile_no;

}

public void setFather_mobile_no(long father_mobile_no) { 

this.father_mobile_no = father_mobile_no;

}


public class Encapsulation_Demo { 

public static void main(String[] args) { 

Encap encaps_obj=new Encap(); encaps_obj.setFather_mobile_no(8976777778l); encaps_obj.setStu_email_id("amit_singhaniya@iitnoida.com"); 


System.out.println("Student_Email_Id:"+encaps_obj.getStu_email_id()+",Father_Mobile_ No:"+encaps_obj.getFather_mobile_no()); 

Student_Email_Id:at_singhaa@iitnoidm,Father_Mobile_No:8976777778

}

Output:

In the above example, variable stu_email_id and father_mobile_no are declared as a private variable and they are present in class Encap. In class Encap setters(setStu_email_id,setFather_mobile_no) and getters (getStu_email_id,getFather_mobile_no) are declared to set and get data.All private data of a class can be set and accessed by setters and getters in java. 


Advantages of Encapsulation: 


Increased Flexibility- We can increase flexibility using encapsulation because we can make variables of a class to read-only or write-only based on our requirement. If we want to make variables read-only we only declare getter methods(No setter methods). And if we want to make variables write only then we only declare setter methods (No getter methods).


Data Hiding- Data hiding is a feature of java in which the user has no information about the internal detail. It will not be visible to the user how the class is storing values in the variables of that class. The user only knows that a variable is passing in the setter method and it is accessible using getter methods.


Reusability- Encapsulation increases the reusability of code and it is easy to change when required. Easy To Test- Encapsulated code is easy for unit testing. 


Interview Questions-


Question 1- Can we access the private variables of a class if yes then it can be in java.

Answer- Yes, In Encapsulation setters and getters are used to set and view variables of a class. So by using the getter method we can view private variables in java.


Question 2-What is encapsulation in Java. Answer- Encapsulation is one of the four and important features of object-oriented programming. It can be defined as a folding up of java code and data into a single unit is known as encapsulation.


Question 3-What is data hiding in Java. Answer- Data hiding is a feature of java in which the user has no information about the internal detail. It will not be visible to the user how the class is storing values in the variables of that class. The user only knows that a variable is passing in the setter method and it is accessible using getter methods. 



study24x7
Write a comment...
Related Posts