Updated April 5, 2023
Introduction to Encapsulation
Encapsulation is a prime concept of object-oriented programming language which emphasizes on making the data secure and abstract. Encapsulation object helps in making the data entry with certain important data only instead of irrelevant data. Encapsulation makes the data secured by presenting to the end-user only the details which are needed. There are three types of encapsulation, namely Member variable encapsulation, Function encapsulation and Class encapsulation, each of which has its own significance with respect to member variables of a class, functions, APIs when it comes to the implementation of any specific application possessing encapsulation.
List of Encapsulation Types
Basically, there are three types of encapsulation which play an essential role in making the overall process successful, which are represented as follows:
- Member Variable Encapsulation Type
- Class Encapsulation Type
- Function Encapsulation Type
1. Member Variable Encapsulation Type
- Member variable encapsulation type mostly deals with the members or the variables of the class.
- These variables are mostly associated with the class object, which includes the members as well as variables.
- These data variables and members must be declared within any personal class when dealing with an object-oriented paradigm.
- It makes the object behave in a way where the manipulation can occur, and changes take place with the retrieval of data values which can make of setters and getters as well.
- Since this type of encapsulation occurs with data members and variables, it makes this encapsulation type substituent called as data participant encapsulation.
- It is not mandatory to use only this kind of encapsulation in terms of members and variables but can also make use of functions and sections also encapsulated.
Example:
This example demonstrates the member type encapsulation where the student roll no is considered, and then the variable is manipulated for making the member variable encapsulation successfully executed, as shown in the output below.
Code:
class Encapsue_Member_Demo
{
private int roll_no;
public int getStdnt_roll_no()
{
return roll_no;
}
public void setStdnt_roll_no(int n_val){
roll_no = n_val;
}
}
public class Encap_Test{
public static void main (String args [])
{
Encapsue_Member_Demo obj_ref = new Encapsue_Member_Demo();
obj_ref.setStdnt_roll_no (202100);
System.out.println ("roll_no: " + obj_ref.getStdnt_roll_no ());
}
}
Output:
2. Class Encapsulation Type
- Class encapsulation type, as the name suggests is part of class which makes use of various composite API’s (Application Programming Interface) which helps in doing the internal Implementation as per the requirement.
- Same and the major logic of the application will be holded by the interface itself.
- This kind of class do not form part of any kind of external API or say else any kind of public graphical User Interface.
- The data within the class and the interface both remains within the class itself thus making it available to members or variables that are present within the class itself. It is not available with the users who are available outside the class or the premises of the API.
- These entire API present within the class is present within the premises also with some ability to modify and customize the parameters present within the API or the class with an ease but within class encapsulation type.
- Somehow or someway it is hidden and stays abstract as it is not available to the outside world thus making it fully inside one location having every detail which is available only till the implementer not the end user who will just get the actual feel not the complete change and is not to be concerned.
Example:
This program demonstrates the class Encapsulation type where the Encapse_Function_Demo comprises of all the main methods with a new class within the body of the class but again due to private and public access this is happening.
Code:
class Encapsue_Function_Demo
{
private int roll_no;
private int getStdnt_roll_no()
{
return roll_no;
}
private void setStdnt_roll_no(int n_val){
roll_no = n_val;
}
}
class StudentInfo{
public String student_name;
void setstudent_name(float n_val);
};
public class Encap_Test{
public static void main (String args [])
{
Encapsue_Function_Demo obj_ref = new Encapsue_Function_Demo();
obj_ref.setStdnt_roll_no (202100);
System.out.println ("roll_no: " + obj_ref.getStdnt_roll_no ());
}
}
3. Function Encapsulation Type
- Functional API present as part of Function Encapsulation Type must show the details and references for a particular function only that too within the function only. All the member variables defined or the logic can be modified and changed but should not accessible outside the function that’s what is the beauty of using function encapsulation type.
- Although the classification applied to the function is not that complex and possess most of the functional changes related to the interface which might need for the end customers to understand the feature change or bug fixes thus those minute details without the actual implementation can be forwarded and distributed in order to publish the change.
- With this it is understandable that only the common features can be made public and access to the end user not the entire implementation or details to be entailed further.
- Encapsulation main motto is to hide the implementation and make the relevant information available so the confidentiality to play around with data is also expected. If the information breach comes into picture then the concept of encapsulation is not at all satisfied rather it is being hampered or is not implemented properly.
Example:
This program demonstrates the Encapsue_Function_Demo where the functional API and its associated member variables and interface is tried to access but since it follows the functional encapsulation type then in that case the member variables are defined as private to maintain the confidentiality as shown in the output.
Code:
class Encapsue_Function_Demo
{
private int roll_no;
private int getStdnt_roll_no()
{
return roll_no;
}
private void setStdnt_roll_no(int n_val){
roll_no = n_val;
}
}
public class Encap_Test{
public static void main (String args [])
{
Encapsue_Function_Demo obj_ref = new Encapsue_Function_Demo();
obj_ref.setStdnt_roll_no (202100);
System.out.println ("roll_no: " + obj_ref.getStdnt_roll_no ());
}
}
Output:
Conclusion – Encapsulation Types
Encapsulation type plays an important role in any object-oriented programming language where it helps in giving data a level of confidentiality. This gives the main member variables and member functions the ability to manipulate the data accordingly whenever needed for any implementation. Encapsulation gives lot of flexibility when it comes to the end user for view and checking.
Recommended Articles
We hope that this EDUCBA information on “Encapsulation Types” was beneficial to you. You can view EDUCBA’s recommended articles for more information.