• Teaching
    • Montessori Teacher Training 🔥
    • Yoga Teacher Training
  • Coding
    • Full Stack Development Course
    • Data Science Course
    • Python Programming Course
    • Software Testing Course
    • Architecting on AWS
  • Commerce
    • PwC Edge : Strategic Accounting and Finance Programme
    • ACCA Course
    • SAP Fico Course
    • Practical Accounting Course
    • SAP MM Course
    • HR Management Course in Kerala
  • Technical
    • Structural Design Course
    • Embedded Systems Software Engineering in Kerala
    • MEP Course
    • MEP QS Course
    • BIM Training Course
    • Quantity Survey Course
  • Finacademy
    • Forex Trading Course
    • Stock Market Course
    • Mutual Funds Course in Kerala
  • Digital Marketing
    • Digital Marketing Course
    • Performance Marketing Course
  • Language Learning
    • Spoken English Course
    • German Language Course
    • IELTS Exam Coaching
  • Exam Preparation
    • Kerala PSC Coaching
    • Kerala PSC LGS Coaching
    • KAS Coaching
    • Kerala PSC LDC Coaching
    • Kerala PSC LSGS Coaching
    • KPSC Industries Extension Officer Coaching
    • KPSC Technical Mechanical Coaching
    • KPSC Technical Electrical Coaching
    • Kerala PSC Technical Exam Preparation
    • KPSC Technical Civil Coaching
    • SBI PO Coaching
    • IBPS Clerk Coaching
    • IBPS PO Coaching
    • IBPS SO Coaching
    • Bank Exam Coaching
    • Lecturer in DIET Course
    • Kpsc HSA Coaching
    • KPSC LP UP Assistant Coaching
    • EMRS Coaching
    • KTET Coaching
    • SSC Coaching
    • SSC JE Coaching
    • SSC CGL Coaching
    • SSC CHSL Coaching
    • SSC MTS Coaching
    • UPSC Coaching
  • Health Care
    • DHA Course
    • OET Coaching
  • Other Exams
    • GATE Coaching
    • CAT Coaching
    • MAT Online Coaching
    • KMAT Online Coaching
Entri Blog
No Result
View All Result
Monday, July 14, 2025
  • Kerala PSC
    • Kerala PSC General
      • Kerala PSC Exams
      • KAS Exam
      • Kerala PSC VEO Notification
      • Kerala Police SI
      • Kerala PSC LDC Notification
      • Kerala PSC Village Field Assistant Notification
      • Kerala PSC LD Typist Notification
      • Kerala PSC Food Safety Officer
      • Kerala PSC Excise Inspector Notification
      • Kerala PSC BDO Notification
      • Kerala PSC CPO Notification
      • Kerala PSC LGS Notification
      • Fire and Rescue Officer
      • Women Fire and Rescue Officer
    • Kerala PSC Teaching
      • Kerala PSC HSA
      • Kerala PSC HSST
      • KTET
      • LP/UP Assistant
      • KVS
      • Railway Teacher
    • Kerala PSC Technical
      • LSGD AE
      • Draftsman/Overseer
      • IEO
      • ATP
      • Polytechnic Lecturer
      • AE Electronics
      • Assistant Engineer
  • Banking
    • IBPS PO Notification
    • IBPS Clerk Notification
    • SBI PO Notification
    • SBI Clerk Notification
    • SBI SO Notification
    • SBI Apprentice Notification
    • Canara Bank PO Notification
    • Indian Bank PO Notification
    • RBI Assistant Notification
    • RBI Office Attendant Notification
    • IBPS RRB Notification
    • IBPS RRB Office Assistant Notification
  • Skilling
    • Coding
    • Spoken English
    • Stock Market
      • Share Market Today News
      • Share Market Analysis
    • Digital Marketing
    • Accounting Course
    • SAP
    • OET
    • German
    • IELTS
    • Quantity Survey
    • MEP
    • Structural Design
    • Montessori
    • Yoga Teaching
    • Oil and Gas
  • Courses
    • Data Science Course
      • Data Science Malayalam
      • Data Science Course in Tamil
      • Data Science Course in Telugu
      • Data Science Training in Kochi
      • Data Science Training in Trivandrum
      • Data Science Course in Calicut
      • Data Science Training in Thrissur
    • Data Analytics Course
      • Data Analytics Course in Malayalam
      • Data Analytics Course in Tamil
      • Data Analytics Course in Telugu
    • BIM Course in Kerala
    • Quantity Surveying Course
      • Quantity Surveyor Course in Tamil
      • Quantity Surveying Course in Telugu
      • Quantity Surveyor Course in Hyderabad
      • Quantity Surveying Course in Chennai
      • Quantity Surveying Course in Vijayawada
      • Quantity Surveying Course in Guntur
      • Quantity Surveying Course in Warangal
      • Quantity Surveying Course in Karimnagar
    • Structural Design Course
      • Steel Structure Design Course
      • RCC Design Course
    • MEP Course
      • MEP Quantity Surveying Course
      • MEP Design Course in Kerala
      • HVAC Course
      • MEP Course in Kochi
    • Embedded System Course
    • Full Stack Developer Course
      • Full Stack Development Malayalam
      • Full Stack Development Hindi
      • Full Stack Development Tamil
      • Full Stack Development Telugu
      • Full Stack Development Kannada
    • Stock Market Course
      • Stock Market Course in Malayalam
      • Stock Market Course in Tamil
      • Stock Market Course in Kannada
      • Options Trading Course
    • Mutual Funds Course
    • Forex Trading Course
    • Spoken English Course
      • Spoken English Course in Malayalam
      • Spoken English Course in Hindi
      • Spoken English Course in Telugu
      • Spoken English Course in Tamil
      • Spoken English Course in Kannada
    • Python Programming Course
    • AI Powered Flutter
    • Ui Ux Designer Course
    • Software Testing Course
      • Software Testing Course in Malayalam
      • Software Testing Course in Tamil
      • Software Testing Course in Telugu
    • AWS Solutions Architect Associate Course
    • PwC Edge : Strategic Accounting and Finance Programme
    • Practical Accounting Course
      • Accounting Course in Malayalam
      • Accounting Course in Tamil
      • Tally Course
      • Taxation Course
      • UAE Accounting
      • GST Course
      • Zoho Books Training
      • GST Practitioner Course in Kerala
      • GCC VAT Course
    • ACCA Training Online
      • ACCA Course in Kerala
    • SAP Training Courses
      • SAP FICO Course
      • SAP MM Course
      • SAP SD Training
      • SAP FICO Course in Tamil
      • SAP MM Course in Tamil
    • HR Management Course
    • Performance Marketing Course
    • Mern Stack Developer Course
      • Full Stack Developer Course in Kochi
      • Full Stack Developer Course in Trivandrum
      • Full Stack Developer Course in Calicut
      • Full Stack Developer Course in Pune
      • Full Stack Developer Course in Bangalore
      • Full Stack Developer Course in Hyderabad
      • Full Stack Developer Course in Chennai
      • Full Stack Developer Course in Indore
      • Full Stack Developer Course in Jaipur
      • Full Stack Developer Course in Coimbatore
    • Digital Marketing Training
      • Digital Marketing Course in Kochi
      • Digital Marketing Course in Trivandrum
      • Digital Marketing Course in Calicut
      • Digital Marketing Course in Kollam
      • Digital Marketing Course in Thrissur
      • Digital Marketing Course in Kottayam
      • Digital Marketing Course in Kannur
    • Montessori Teacher Training
      • Montessori Teacher Training in Calicut
      • Montessori Teacher Training in Kochi, Ernakulam
      • Montessori Teacher Training in Trivandrum
      • Montessori Teacher Training in Kollam
      • Montessori Teacher Training in Malayalam
      • Montessori Teacher Training in Kannada
    • Other Courses
      • German Language Course
      • OET Coaching
        • OET for Pharmacist
        • OET for Nurses
      • DHA Course
      • MOH Exam Coaching
      • Nurses Recruitment Abroad
      • FMGE Coaching
      • ChatGPT Course
      • Forex Trading Course
      • Yoga Teacher Training Course
      • Diploma in Early Childhood Care and Education
      • Business Administration & Hospital Management Course
      • Hospital and Healthcare Administration
      • AI for Teachers Course
  • Others
    • Railway
      • RRB Technician Notification
      • RRB JE Notification
      • RRB ALP Notification
      • RRB NTPC Notification
      • RRB RPF Notification
      • RRB RPF SI Notification
      • RRB RPF Constable Notification
      • RRB Group D Notification
      • RRB RPSF SI Notification
      • RRB SSE Notification
    • SSC
      • SSC CPO Notification
      • SSC MTS Notification
      • SSC CGL Notification
      • SSC CHSL Notification
      • SSC GD Constable Notification
      • SSC Stenographer Notification
      • SSC JE Notification
      • SSC CAPF Notification
      • SSC JHT Notification
      • Delhi Police Constable Notification
      • Delhi Police SI Notification
    • GATE
    • MAT
    • KMAT
      • KMAT Kerala 2024
    • TET
      • APTET
      • CTET
      • DSSSB
      • Karnataka TET
      • Kerala TET
        • KTET Notification
      • KVS
      • MPTET
      • SUPER TET
      • TNTET
      • TSTET
      • UPTET
    • UPSC
      • UPSC CAPF Notification
      • UPSC CDS Notification
      • UPSC NDA Notification
      • UPSC Civil service Notification
  • Free Learn
    • Free Courses
      • Free Coding Courses
        • C Langauge Courses
        • Free Data Science Courses
        • Free Python Programming Courses
        • Free Full Stack Developer Courses
      • Free Accounting Courses
      • Free Digital Marketing Courses
        • Free SEO Courses
        • Web Development for Marketers
      • Engineering Upskilling Courses
      • Free Hospital Administration Courses
      • Free OET Training
      • Free SAP Courses
      • Free German Courses
      • Free IELTS Courses
      • Free Teachers Training Courses
      • Free Trading Courses
        • Free Stock Market Courses in Malayalam
        • Free Options Trading Course Malayalam
        • Free Forex Trading Courses Malayalam
        • Free Personal Finance Courses Malayalam
    • Question Answer
    • Aptitude Questions
    • Webinars
  • Test Prep
    • Kerala PSC Coaching
      • KPSC Degree Level Exam Preparation
      • Kerala PSC 12th Level Exam Preparation
      • Kerala PSC 10th Level Exam Preparation
      • KAS Coaching
      • Kerala PSC LDC Coaching
      • Kerala PSC LD Typist Coaching
      • Kerala PSC Fireman Coaching
      • KSFE Assistant Exam Preparation
      • Secretariat Office Attendant Coaching
      • Secretariat Assistant Coaching
      • Kerala PSC SI Exam Coaching
      • Khadi Board LDC Exam Preparation
      • Devaswom Board LDC Coaching
      • University LGS Exam Preparation
      • University Assistant Exam Coaching
      • KPSC Scientific Officer Exam Coaching
      • KPSC Probation Officer Grade II
      • KPSC Food Safety Officer Coaching
      • KWA Sanitary Chemist Coaching
      • KPSC Diary Farm Instructor Exam Coaching
      • Kerala PSC KWA Lab Assistant Exam Coaching
      • Kerala PSC ICDS Supervisor Exam Coaching
      • KPSC Finger Print Searcher Coaching
      • Kerala PSC Welfare Officer Gr. II Exam Coaching
      • KPSC Assistant Dental Surgeon Coaching
    • KPSC Teaching Exams
      • HSA Exam Preparation
      • KPSC HSA Social Science Coaching
      • KPSC HSA Physical Science Coaching
      • KPSC HSA English Coaching
      • KPSC Junior Language Teacher Hindi
      • KTET Coaching
      • SET Coaching
      • LP/UP Assistant Exam Coaching
      • HSST Exam Preparation
      • HSST Commerce Exam Coaching
      • Kerala PSC Lecturer in Diet Coaching
      • KVS PGT/TGT Coaching
    • Kerala PSC Technical Exams Coaching
      • Assistant Engineer Electrical Exam Coaching
      • Assistant Engineer Electronics Coaching
      • Assistant Engineer Mechanical Coaching
      • Kerala PSC Industries Extension Officer Coaching
      • KPSC LSGD AE Exam Coaching
      • KPSC Civil Engineering Exam Coaching
      • KPSC Mechanical Engineering Exam Coaching
      • KPSC Electrical Engineering Exam Coaching
      • KPSC Electronics Engineering Exam Coaching
      • KPSC Technical Computer Science Coaching
      • KPSC Tradesman Turning Coaching
      • Kerala PSC Overseer Exam Coaching
      • KPSC Assistant Town Planner Coaching
      • Lecturer in Polytechnic Electronics Coaching
    • Teaching Exams Coaching
      • CSIR NET Exam Coaching
      • EMRS Coaching
      • CTET Exam Online Coaching
    • IELTS Training Online
    • Bank Exam Coaching Online
      • SEBI Grade A Coaching
      • IBPS SO Online Coaching
      • IBPSC PO Online Coaching
      • IBPSC Clerk Online Coaching
      • SBI PO Coaching
      • SBI Clerk Online Coaching
      • SBI Apprentice Class
      • SBI SO Online Coaching
      • RBI Grade B Coaching
      • RBI Assistant Coaching
      • CSEB Exam Coaching
      • IBPS RRB Coaching
      • IBPS RRB PO Coaching
      • Canara Bank Po Exam Coaching
      • Kerala Gramin Bank Coaching
      • Kerala Bank Exam Coaching
        • Kerala Bank Clerk Exam Coaching
        • Kerala Bank Office Attendant Exam Coaching
        • Kerala Bank Assistant Manager Exam Coaching
      • JCI Exam Coaching
      • NABARD Grade A Coaching
      • LIC AAO Coaching Online
    • UPSC Online Coaching
      • UPSC CAPF Online Coaching
    • SSC Online Coaching
      • SSC JE Online Coaching
      • SSC CGL Coaching
      • SSC CGL AAO Coaching
      • SSC CHSL Coaching
      • SSC CGL CBI Inspector Coaching
      • SSC MTS Coaching
      • SSC Stenographer Coaching
      • SSC GD Online Coaching
    • RRB Exams Coaching
      • RRB RPF Exam Coaching
      • RRB NTPC Exam Coaching
      • RRB JE Online Coaching
      • RRB JE Electronics Coaching
      • RRB ALP Exam Coaching
      • RRB Technician Exam Coaching
    • CAT Online Coaching
    • CMAT Online Coaching
    • XAT Exam Coaching
    • GATE Online Coaching
      • GATE CE Online Coaching
      • GATE ME Online Coaching
      • GATE EE Online Coaching
    • NMAT Exam Coaching
      • AAI ATC Jr Executive Coaching
    • Cochin Shipyard Executive Trainee Coaching
    • BEL Probationary Engineer Coaching
    • HPCL Junior Executive Coaching
Free Courses
Entri Blog
  • HTML Tutorial
  • DSA Tutorials
  • Tutorials in Hindi
    • HTML Tutorial for Beginners in Hindi
    • Python Tutorial for Beginners in Hindi
    • GIT and GITHUB Tutorial for Beginners in Hindi
  • Tutorials in Tamil
    • JavaScript Tutorial in Tamil
  • Tutorials in Telugu
    • HTML Tutorial For Beginners in Telugu
    • CSS Tutorial for Beginners in Telugu
    • Bootstrap Tutorial for Beginner in Telugu
  • Tutorials in Kannada
    • HTML Tutorial For Beginners in Kannada
entri exclusive offers entri exclusive offers
Home Articles

What is Encapsulation in Java and How to Implement It

by Feeba Mahin
November 23, 2022
in Articles, Java Programming
Why To Choose The Full Stack Development For Modern Business.
Share on FacebookShare on WhatsAppShare on Telegram

Table of Contents

  • What is Encapsulation in Java?
  • Need for Encapsulation in Java
  • Data Hiding in Java
  • Data Hiding vs. Encapsulation in Java
  • Getter and Setter Methods
  • Benefits of Encapsulation in Java
  • Java Encapsulation Example: Using Getter-Setter
  • Advantages of Encapsulation in java
  • Conclusion
Encapsulation in Java is a powerful mechanism for storing the data members and data methods of a class together. It is done in the form of a secure field accessible by only the members of the same class.

Python and Machine Learning Rectangle

Grab the opportunity to learn Python with Entri! Click Here

What is Encapsulation in Java?

1: What is the default value of a boolean in Java?

2: What is the TreeSet Interface?

3: Which loop construct in Java best suits when the number of iterations is known?

4: What is the size of a byte variable?

5: Which of the following is thread safe?

    Fill out the form to see the results



    Ever wondered how much you really know? It's time to put your brain to the test!

    Encapsulation in Java refers to integrating data (variables) and code (methods) into a single unit. In encapsulation, a class’s variables are hidden from other classes and can only be accessed by the methods of the class in which they are found.

    java

    Encapsulation in Java is an object-oriented procedure of combining the data members and data methods of the class inside the user-defined class. It is important to declare this class as private.

    Next, we will understand the Syntax to be followed while implementing encapsulation in Java.

    Syntax:

    <Access_Modifier> class <Class_Name> {

    private <Data_Members>;

    private <Data_Methods>;

    }

    For enhancing the understanding of the encapsulation process, let us go through the following sample program.

    Example:

    package dc;

    public class c

    {

    public static void main (String[] args)

    {

    Employee e = new Employee();

    e.setName(“Robert”);

    e.setAge(33);

    e.setEmpID(1253);

    System.out.println(“Employee’s name: ” + e.getName());

    System.out.println(“Employee’s age: ” + e.getAge());

    System.out.println(“Employee’s ID: ” + e.getEmpID());

    }

    }

    package dc;

    public class Employee {

    private String Name;

    private int EmpID;

    private int Age;

    public int getAge() {

    return Age;

    }

    public String getName() {

    return Name;

    }

    public int getEmpID() {

    return EmpID;

    }

    public void setAge(int newAge) {

    Age = newAge;

    }

    public void setName(String newName) {

    Name = newName;

    }

    public void setRoll(int newEmpID) {

    EmpID = newEmpID;

    }

    public void setEmpID(int EmpID) {

    }

    }

    //

    Output:

    Employee’s name: Robert

    Employee’s age: 33

    Employee’s ID: 1253

    Explore Free Coding Courses !

    Take your first step toward mastering in-demand skills, acing interviews, and securing top-tier jobs with Entri's free coding courses.

    👉 Explore Free Courses Now

    Need for Encapsulation in Java

    Encapsulation improvises the procedure of coding to a whole new level. We need encapsulation in Java for various reasons. They are stated below.

    Encapsulation

    Better Control

    Encapsulation provides ultimate control over the data members and data methods inside the class.

    Getter and Setter

    The standard IDEs provide in-built support for ‘Getter and Setter’ methods, which increases the programming pace.

    Security

    Encapsulation prevents access to data members and data methods by any external classes. The encapsulation process improves the security of the encapsulated data.

    Flexibility

    Changes made to one part of the code can be successfully implemented without affecting any other part of the code.

    Data Hiding in Java

    Data hiding is a procedure done to avoid access to the data members and data methods and their logical implementation. Data hiding can be done by using the access specifiers. We have four access specifiers, which are as follows.

    Default

    Default is the first line of data hiding. If any class in Java is not mentioned with an access specifier, then the compiler will set ‘default’ as the access specifier. The access specifications of default are extremely similar to that of the public access specifier.

    Public

    The public access specifier provides the access specifications to a class so that it can be accessed from anywhere within the program.

    Example:

    package Simplilearn;

    class vehicle {

    public int tires;

    public void display() {

    System.out.println(“I have a vehicle.”);

    System.out.println(“It has ” + tires + ” tires.”);

    }

    }

    public class Display {

    public static void main(String[] args) {

    vehicle veh = new vehicle();

    veh.tires = 4;

    veh.display();

    }

    }

    //Output:

    I have a vehicle.

    It has four tires.

    Private

    The private access specifier provides access to the data members, and the data methods limit to the class itself.

    Example:

    package Simplilearn;

    class Student {

    private int rank;

    public int getRank() {

    return rank;

    }

    public void setRank(int rank) {

    this.rank = rank;

    }

    }

    public class school {

    public static void main(String[] args) {

    Student s = new Student();

    s.setRank(1022);

    System.out.println(“Student rank is ” + s.getRank());

    }

    }

    //Output:

    Student rank is 1022

    Learn to code from industry experts! Enroll here

    Protected

    The protected access specifier protects the class methods and members similar to the private access specifier. The main difference is that the access is limited to the entire package, unlike only a class with the private access specifier.

    Example:

    package Simplilearn;

    class human {

    protected String stream;

    protected void display() {

    System.out.println(“Hello, I am a ” + stream + ” Student”);

    }

    }

    public class Student extends human {

    public static void main(String[] args) {

    Student s = new Student();

    s.stream = “Computer Science and Engineering Technology”;

    s.display();

    }

    }

    //Output:

    Hello, I am a Computer Science and Engineering Technology Student

    Data Hiding vs. Encapsulation in Java

    Data Hiding Data Encapsulation
    Data hiding can be considered as the parent process Encapsulation is a sub-process of data hiding
    Access specifier is always private Access specifier can be private and public
    Data hiding is about hiding method implementation Encapsulation is about combining methods with data members
    The main motto is to hide data and its implementation The main motto is to combine data and their methods

    Explore Free Coding Courses !

    Take your first step toward mastering in-demand skills, acing interviews, and securing top-tier jobs with Entri's free coding courses.

    👉 Explore Free Courses Now

    Getter and Setter Methods

    Getter and setter techniques are commonly referred to in object-oriented programming languages. An attribute can be retrieved using a getter method, and it can also be changed using a setter method, as indicated by the names. Your implementation methods will determine if an attribute may be read and updated. Additionally, you may choose whether or not the attribute is read-only or wholly hidden from view.

    Example 1

    Following is an example of encapsulation in java –

    /* File name : EncapTest.java */

    public class EncapTest {

    private String name;

    private String idNum;

    private int age;

    public int getAge() {

    return age;

    }

    public String getName() {

    return name;

    }

    public String getIdNum() {

    return idNum;

    }

    public void setAge( int newAge) {

    age = newAge;

    }

    public void setName(String newName) {

    name = newName;

    }

    public void setIdNum( String newId) {

    idNum = newId;

    }

    }

    The public setXXX() and getXXX() methods provide access to the EncapTest class’s instance variables.

    The terms “getters” and “setters” are often used to describe these techniques. As a result, every class that needs to access the variables should use these getters and setters.

    The variables of the encap test class can be accessed using the following program –

    /* File name : RunEncap.java */

    public class RunEncap {

    public static void main(String args[]) {

    EncapTest encap = new EncapTest();

    encap.setName(“James”);

    encap.setAge(20);

    encap.setIdNum(“12343ms”);

    System.out.print(“Name : ” + encap.getName() + ” Age : ” + encap.getAge());

    }

    }

    It will result in the following outcome:

    Output:

    Name: James Age: 20

    The CoffeeMachine Example

    Encapsulation, or “Information Hiding,” refers to the practice of concealing the details of an object’s internal processes. It’s nothing more than a simple data masking technique.

    When the CoffeeMachine class was developed, the encapsulation method was implemented. The current state of the CoffeeMachine object is recorded in the attributes configMap, beans, grinder, and brewingUnit.

    The methods brewCoffee, brewEspresso, brewFilterCoffee, and addBeans are available to perform various operations on these properties.

    Code:

    import java.util.HashMap;

    import java.util.Map;

    public class CoffeeMachine {

    private Map configMap;

    private Map beans;

    private Grinder grinder;

    private BrewingUnit brewingUnit;

    public CoffeeMachine(Map beans) {

    this.beans = beans;

    this.grinder = new Grinder();

    this.brewingUnit = new BrewingUnit();

    this.configMap = new HashMap();

    this.configMap.put(CoffeeSelection.ESPRESSO, new Configuration(8, 28));

    this.configMap.put(CoffeeSelection.FILTER_COFFEE, new Configuration(30, 480));

    }

    public Coffee brewCoffee(CoffeeSelection selection) throws CoffeeException {

    switch (selection) {

    case FILTER_COFFEE:

    return brewFilterCoffee();

    case ESPRESSO:

    return brewEspresso();

    default:

    throw new CoffeeException(“CoffeeSelection [” + selection + “] not supported!”);

    }

    }

    private Coffee brewEspresso() {

    Configuration config = configMap.get(CoffeeSelection.ESPRESSO);

    // grind the coffee beans

    GroundCoffee groundCoffee = this.grinder.grind(

    this.beans.get(CoffeeSelection.ESPRESSO), config.getQuantityCoffee());

    // brew an espresso

    return this.brewingUnit.brew(CoffeeSelection.ESPRESSO,

    groundCoffee, config.getQuantityWater());

    }

    private Coffee brewFilterCoffee() {

    Configuration config = configMap.get(CoffeeSelection.FILTER_COFFEE);

    // grind the coffee beans

    GroundCoffee groundCoffee = this.grinder.grind(

    this.beans.get(CoffeeSelection.FILTER_COFFEE), config.getQuantityCoffee());

    // brew a filter coffee

    return this.brewingUnit.brew(CoffeeSelection.FILTER_COFFEE,

    groundCoffee, config.getQuantityWater());

    }

    public void addBeans(CoffeeSelection sel, CoffeeBean newBeans) throws CoffeeException {

    CoffeeBean existingBeans = this.beans.get(sel);

    if (existingBeans != null) {

    if (existingBeans.getName().equals(newBeans.getName())) {

    existingBeans.setQuantity(existingBeans.getQuantity() + newBeans.getQuantity());

    } else {

    throw new CoffeeException(“Only one kind of beans supported for each CoffeeSelection.”);

    }

    } else {

    this.beans.put(sel, newBeans);

    }

    }

    }

    The Coffee class exemplifies the process for hiding information and represents a beverage produced by a CoffeeMachine. The Coffee Vending Machine encapsulates internal processes and ingredients (data).

    In Object Oriented languages, encapsulation is provided via modifiers like “private” and “protected.” Transient and volatile can likewise serve as encapsulating modifiers, but only in specific contexts.

    In addition to OOP languages, encapsulation in Java can be used in other languages. Webservices, SOA (Service Oriented Architecture), and other cutting-edge technologies are part of the notion. You can see encapsulation in real-world objects if you look closely; this is what Object Oriented Programming seeks to mimic.

    Benefits of Encapsulation in Java

    Implementing the process of encapsulation in Java has proven to be highly effective and beneficial while programming in real-time. The following are the significant benefits of encapsulation.

    • A class can have complete control over its data members and data methods.
    • The class will maintain its data members and methods as read-only.
    • Data hiding prevents the user from the complex implementations in the code.
    • The variables of the class can be read-only or write-only as per the programmer’s requirement.
    • Encapsulation in Java provides an option of code-reusability.
    • Using encapsulation will help in making changes to an existing code quickly.
    • Unit testing a code designed using encapsulation is elementary.
    • Standard IDEs have the support of getters and setters; this makes coding even faster.

    Java Encapsulation Example: Using Getter-Setter

    Given below is an example program that demonstrate the way encapsulation is achieved in java.
    • In the example given below, we can see that attributes firstNumber and secondNumber are private to the CalculatorVO.java class and thus it cannot be accessed directly by any outside class.
    • Hence the data is getting wrapped using concept of encapsulation and could only be read or written using public getter and setter methods provided by the class.
    
    
    package calculator;
    
    public class CalculatorVO {
    
          private int firstNumber;
          private int secondNumber;
    
          public int getFirstNumber() {
                return firstNumber;
          }
    
          public void setFirstNumber(int firstNumber) {
                this.firstNumber = firstNumber;
          }
    
          public int getSecondNumber() {
                return secondNumber;
          }
    
          public void setSecondNumber(int secondNumber) {
                this.secondNumber = secondNumber;
          }
    
    }
    

    Advantages of Encapsulation in java

    Listed below are the advantages of encapsulation in java:

    • Using encapsulation helps us to make our variable read only or write only. For example we have declared our variables firstNumber and secondNumber private. To make these variables read only we can remove the setter methods for both of them. To make it write only, we can remove the getter method. This in turn provide programming flexibility.
    • Encapsulation also helps in data hiding. For instance, in our above code snippet, variables firstNumber and secondNumber is private and thus hidden from outside world. No one can access them directly. The only mean to access them is though their getter and setter method.
    • Encapsulated code is easy to test and maintain.

    Conclusion

    As an object-oriented programming principle, Java encapsulation describes the grouping of data and methods that interact with this data into a single unit.

    It’s frequently employed as a means of concealing sensitive data. This approach restricts external access to specific attributes while still allowing them to be accessed by members of the current class via public getter and setter methods. You can specify which attributes can be read or updated using these methods and validate a new value before changing an attribute using them.

    Python and Machine Learning Rectangle

    In order to protect user data, encapsulation provides the fundamental property of hiding data. OOP best practices like encapsulation are beneficial when paired with an APM solution like Retrace for error detection.

    Learn Coding in your Language! Enroll Here!

    Share63SendShare

    Feeba Mahin

    Related Posts

    Preschool Teacher Resume Tips & Sample Formats
    Articles

    Preschool Teacher Resume Tips & Sample Formats

    July 12, 2025
    Preschool Teacher Salary in Kerala
    Articles

    Preschool Teacher Salary in Kerala

    July 12, 2025
    Montessori Preschool Teacher
    Articles

    Skills Every Preschool Teacher Must Have

    July 12, 2025
    Next Post
    Haryana PSC ADO Job Profile 2022 - Roles, Responsibilities, Qualification

    Haryana PSC ADO Job Profile 2022 - Roles, Responsibilities, Qualification

    Latest Posts

    • Preschool Teacher Resume Tips & Sample Formats
    • Preschool Teacher Salary in Kerala
    • Skills Every Preschool Teacher Must Have
    • Best UI/UX Courses for Graphic Designers
    • UI/UX Certification vs Degree: What Should Graphic Designers Choose?

    Trending Posts

    • Kerala PSC Fire Woman Answer Key 2025 PDF Download, Get Link

      Kerala PSC Fire Woman Answer Key 2025 PDF Download, Get Link

      316 shares
      Share 126 Tweet 79
    • List of 28 States of India and their Capitals and Languages 2025 – PDF Download

      152701 shares
      Share 61078 Tweet 38174
    • List of Government Banks in India 2025: All you need to know

      66786 shares
      Share 26714 Tweet 16697
    • TNPSC Group 2 Posts and Salary Details 2025

      40467 shares
      Share 16187 Tweet 10117
    • New Map of India with States and Capitals 2025

      35829 shares
      Share 14331 Tweet 8957

    Courses

    • Data Science Course
    • Full Stack Developer Course
    • Software Testing Course
    • Data Science Course in Malayalam
    • Full Stack Developer Course in Malayalam
    • Full Stack Developer Course in Hindi
    • Full Stack Developer Course in Tamil
    • Full Stack Developer Course in Telugu
    • Full Stack Developer Course in Kannada
    • Python Programming Course
    • Practical Accounting Course
    • Structural Design Course
    • Quantity Surveying Course
    • Embedded Systems Course in Kerala
    • BIM Course
    • MEP Course
    • MEP QS Course
    • Stock Market Course
    • Stock Market Course in Malayalam
    • Stock Market Course in Tamil
    • Stock Market Course in Kannada
    • Forex Trading Course
    • Mutual Funds Course
    • PwC Edge : Strategic Accounting and Finance Programme
    • SAP FICO Training
    • SAP MM Training
    • SAP SD Training
    • ACCA Online Course
    • HR Management Course

    Company

    • Become a teacher
    • Login to Entri Web

    Spoken English Courses

    • Spoken English Course
    • Spoken English Course in Malayalam
    • Spoken English Course in Hindi
    • Spoken English Course in Tamil
    • Spoken English Course in Telugu
    • Spoken English Course in Kannada
    • Spoken English Course for Housewives
    • Spoken English Course for Working Professionals
    • Spoken English Course for School Students
    • Spoken English Course for College Students
    • Spoken English Course for Job Seekers
    • AI Powered Spoken English Course

    Quick Links

    • Explore Free Courses
    • Aptitude Questions
    • Articles
    • Videos
    • Entri Daily Quiz Practice
    • Current Affairs & GK
    • News Capsule – eBook
    • Preparation Tips
    • Kerala PSC Gold
    • Entri Skilling
    • Kerala PSC

    Other Courses

    • OET Coaching Classes
    • DHA Course
    • Nurse Recruitment Abroad
    • Montessori Teachers Training
    • Performance Marketing Course
    • German Language Course
    • Yoga Teacher Training Course
    • Digital Marketing Course
    • Options Trading Course
    • German Language A1 Course
    • German Language A2 Course
    • German Language B1 Course
    • German Language B2 Course
    • Diploma in Early Childhood Care and Education
    • Hospital and Healthcare Administration
    • Business Administration & Hospital Management
    • Hospital and Healthcare Administration

    Popular Exam

    • IBPS Exam
    • SBI Exam
    • Railway RRB Exam
    • Kerala PSC
    • Tamil Nadu PSC
    • Telangana PSC
    • Andhra Pradesh PSC
    • MPPSC
    • UPPSC
    • Karnataka PSC
    • Staff Selection Commission Exam
    • GATE

    © 2023 Entri.app - Privacy Policy | Terms of Service

    No Result
    View All Result
    • Kerala PSC
      • Kerala PSC General
        • Kerala PSC Exams
        • KAS Exam
        • Kerala PSC VEO Notification
        • Kerala Police SI
        • Kerala PSC LDC Notification
        • Kerala PSC Village Field Assistant Notification
        • Kerala PSC LD Typist Notification
        • Kerala PSC Food Safety Officer
        • Kerala PSC Excise Inspector Notification
        • Kerala PSC BDO Notification
        • Kerala PSC CPO Notification
        • Kerala PSC LGS Notification
        • Fire and Rescue Officer
        • Women Fire and Rescue Officer
      • Kerala PSC Teaching
        • Kerala PSC HSA
        • Kerala PSC HSST
        • KTET
        • LP/UP Assistant
        • KVS
        • Railway Teacher
      • Kerala PSC Technical
        • LSGD AE
        • Draftsman/Overseer
        • IEO
        • ATP
        • Polytechnic Lecturer
        • AE Electronics
        • Assistant Engineer
    • Banking
      • IBPS PO Notification
      • IBPS Clerk Notification
      • SBI PO Notification
      • SBI Clerk Notification
      • SBI SO Notification
      • SBI Apprentice Notification
      • Canara Bank PO Notification
      • Indian Bank PO Notification
      • RBI Assistant Notification
      • RBI Office Attendant Notification
      • IBPS RRB Notification
      • IBPS RRB Office Assistant Notification
    • Skilling
      • Coding
      • Spoken English
      • Stock Market
        • Share Market Today News
        • Share Market Analysis
      • Digital Marketing
      • Accounting Course
      • SAP
      • OET
      • German
      • IELTS
      • Quantity Survey
      • MEP
      • Structural Design
      • Montessori
      • Yoga Teaching
      • Oil and Gas
    • Courses
      • Data Science Course
        • Data Science Malayalam
        • Data Science Course in Tamil
        • Data Science Course in Telugu
        • Data Science Training in Kochi
        • Data Science Training in Trivandrum
        • Data Science Course in Calicut
        • Data Science Training in Thrissur
      • Data Analytics Course
        • Data Analytics Course in Malayalam
        • Data Analytics Course in Tamil
        • Data Analytics Course in Telugu
      • BIM Course in Kerala
      • Quantity Surveying Course
        • Quantity Surveyor Course in Tamil
        • Quantity Surveying Course in Telugu
        • Quantity Surveyor Course in Hyderabad
        • Quantity Surveying Course in Chennai
        • Quantity Surveying Course in Vijayawada
        • Quantity Surveying Course in Guntur
        • Quantity Surveying Course in Warangal
        • Quantity Surveying Course in Karimnagar
      • Structural Design Course
        • Steel Structure Design Course
        • RCC Design Course
      • MEP Course
        • MEP Quantity Surveying Course
        • MEP Design Course in Kerala
        • HVAC Course
        • MEP Course in Kochi
      • Embedded System Course
      • Full Stack Developer Course
        • Full Stack Development Malayalam
        • Full Stack Development Hindi
        • Full Stack Development Tamil
        • Full Stack Development Telugu
        • Full Stack Development Kannada
      • Stock Market Course
        • Stock Market Course in Malayalam
        • Stock Market Course in Tamil
        • Stock Market Course in Kannada
        • Options Trading Course
      • Mutual Funds Course
      • Forex Trading Course
      • Spoken English Course
        • Spoken English Course in Malayalam
        • Spoken English Course in Hindi
        • Spoken English Course in Telugu
        • Spoken English Course in Tamil
        • Spoken English Course in Kannada
      • Python Programming Course
      • AI Powered Flutter
      • Ui Ux Designer Course
      • Software Testing Course
        • Software Testing Course in Malayalam
        • Software Testing Course in Tamil
        • Software Testing Course in Telugu
      • AWS Solutions Architect Associate Course
      • PwC Edge : Strategic Accounting and Finance Programme
      • Practical Accounting Course
        • Accounting Course in Malayalam
        • Accounting Course in Tamil
        • Tally Course
        • Taxation Course
        • UAE Accounting
        • GST Course
        • Zoho Books Training
        • GST Practitioner Course in Kerala
        • GCC VAT Course
      • ACCA Training Online
        • ACCA Course in Kerala
      • SAP Training Courses
        • SAP FICO Course
        • SAP MM Course
        • SAP SD Training
        • SAP FICO Course in Tamil
        • SAP MM Course in Tamil
      • HR Management Course
      • Performance Marketing Course
      • Mern Stack Developer Course
        • Full Stack Developer Course in Kochi
        • Full Stack Developer Course in Trivandrum
        • Full Stack Developer Course in Calicut
        • Full Stack Developer Course in Pune
        • Full Stack Developer Course in Bangalore
        • Full Stack Developer Course in Hyderabad
        • Full Stack Developer Course in Chennai
        • Full Stack Developer Course in Indore
        • Full Stack Developer Course in Jaipur
        • Full Stack Developer Course in Coimbatore
      • Digital Marketing Training
        • Digital Marketing Course in Kochi
        • Digital Marketing Course in Trivandrum
        • Digital Marketing Course in Calicut
        • Digital Marketing Course in Kollam
        • Digital Marketing Course in Thrissur
        • Digital Marketing Course in Kottayam
        • Digital Marketing Course in Kannur
      • Montessori Teacher Training
        • Montessori Teacher Training in Calicut
        • Montessori Teacher Training in Kochi, Ernakulam
        • Montessori Teacher Training in Trivandrum
        • Montessori Teacher Training in Kollam
        • Montessori Teacher Training in Malayalam
        • Montessori Teacher Training in Kannada
      • Other Courses
        • German Language Course
        • OET Coaching
        • DHA Course
        • MOH Exam Coaching
        • Nurses Recruitment Abroad
        • FMGE Coaching
        • ChatGPT Course
        • Forex Trading Course
        • Yoga Teacher Training Course
        • Diploma in Early Childhood Care and Education
        • Business Administration & Hospital Management Course
        • Hospital and Healthcare Administration
        • AI for Teachers Course
    • Others
      • Railway
        • RRB Technician Notification
        • RRB JE Notification
        • RRB ALP Notification
        • RRB NTPC Notification
        • RRB RPF Notification
        • RRB RPF SI Notification
        • RRB RPF Constable Notification
        • RRB Group D Notification
        • RRB RPSF SI Notification
        • RRB SSE Notification
      • SSC
        • SSC CPO Notification
        • SSC MTS Notification
        • SSC CGL Notification
        • SSC CHSL Notification
        • SSC GD Constable Notification
        • SSC Stenographer Notification
        • SSC JE Notification
        • SSC CAPF Notification
        • SSC JHT Notification
        • Delhi Police Constable Notification
        • Delhi Police SI Notification
      • GATE
      • MAT
      • KMAT
        • KMAT Kerala 2024
      • TET
        • APTET
        • CTET
        • DSSSB
        • Karnataka TET
        • Kerala TET
        • KVS
        • MPTET
        • SUPER TET
        • TNTET
        • TSTET
        • UPTET
      • UPSC
        • UPSC CAPF Notification
        • UPSC CDS Notification
        • UPSC NDA Notification
        • UPSC Civil service Notification
    • Free Learn
      • Free Courses
        • Free Coding Courses
        • Free Accounting Courses
        • Free Digital Marketing Courses
        • Engineering Upskilling Courses
        • Free Hospital Administration Courses
        • Free OET Training
        • Free SAP Courses
        • Free German Courses
        • Free IELTS Courses
        • Free Teachers Training Courses
        • Free Trading Courses
      • Question Answer
      • Aptitude Questions
      • Webinars
    • Test Prep
      • Kerala PSC Coaching
        • KPSC Degree Level Exam Preparation
        • Kerala PSC 12th Level Exam Preparation
        • Kerala PSC 10th Level Exam Preparation
        • KAS Coaching
        • Kerala PSC LDC Coaching
        • Kerala PSC LD Typist Coaching
        • Kerala PSC Fireman Coaching
        • KSFE Assistant Exam Preparation
        • Secretariat Office Attendant Coaching
        • Secretariat Assistant Coaching
        • Kerala PSC SI Exam Coaching
        • Khadi Board LDC Exam Preparation
        • Devaswom Board LDC Coaching
        • University LGS Exam Preparation
        • University Assistant Exam Coaching
        • KPSC Scientific Officer Exam Coaching
        • KPSC Probation Officer Grade II
        • KPSC Food Safety Officer Coaching
        • KWA Sanitary Chemist Coaching
        • KPSC Diary Farm Instructor Exam Coaching
        • Kerala PSC KWA Lab Assistant Exam Coaching
        • Kerala PSC ICDS Supervisor Exam Coaching
        • KPSC Finger Print Searcher Coaching
        • Kerala PSC Welfare Officer Gr. II Exam Coaching
        • KPSC Assistant Dental Surgeon Coaching
      • KPSC Teaching Exams
        • HSA Exam Preparation
        • KPSC HSA Social Science Coaching
        • KPSC HSA Physical Science Coaching
        • KPSC HSA English Coaching
        • KPSC Junior Language Teacher Hindi
        • KTET Coaching
        • SET Coaching
        • LP/UP Assistant Exam Coaching
        • HSST Exam Preparation
        • HSST Commerce Exam Coaching
        • Kerala PSC Lecturer in Diet Coaching
        • KVS PGT/TGT Coaching
      • Kerala PSC Technical Exams Coaching
        • Assistant Engineer Electrical Exam Coaching
        • Assistant Engineer Electronics Coaching
        • Assistant Engineer Mechanical Coaching
        • Kerala PSC Industries Extension Officer Coaching
        • KPSC LSGD AE Exam Coaching
        • KPSC Civil Engineering Exam Coaching
        • KPSC Mechanical Engineering Exam Coaching
        • KPSC Electrical Engineering Exam Coaching
        • KPSC Electronics Engineering Exam Coaching
        • KPSC Technical Computer Science Coaching
        • KPSC Tradesman Turning Coaching
        • Kerala PSC Overseer Exam Coaching
        • KPSC Assistant Town Planner Coaching
        • Lecturer in Polytechnic Electronics Coaching
      • Teaching Exams Coaching
        • CSIR NET Exam Coaching
        • EMRS Coaching
        • CTET Exam Online Coaching
      • IELTS Training Online
      • Bank Exam Coaching Online
        • SEBI Grade A Coaching
        • IBPS SO Online Coaching
        • IBPSC PO Online Coaching
        • IBPSC Clerk Online Coaching
        • SBI PO Coaching
        • SBI Clerk Online Coaching
        • SBI Apprentice Class
        • SBI SO Online Coaching
        • RBI Grade B Coaching
        • RBI Assistant Coaching
        • CSEB Exam Coaching
        • IBPS RRB Coaching
        • IBPS RRB PO Coaching
        • Canara Bank Po Exam Coaching
        • Kerala Gramin Bank Coaching
        • Kerala Bank Exam Coaching
        • JCI Exam Coaching
        • NABARD Grade A Coaching
        • LIC AAO Coaching Online
      • UPSC Online Coaching
        • UPSC CAPF Online Coaching
      • SSC Online Coaching
        • SSC JE Online Coaching
        • SSC CGL Coaching
        • SSC CGL AAO Coaching
        • SSC CHSL Coaching
        • SSC CGL CBI Inspector Coaching
        • SSC MTS Coaching
        • SSC Stenographer Coaching
        • SSC GD Online Coaching
      • RRB Exams Coaching
        • RRB RPF Exam Coaching
        • RRB NTPC Exam Coaching
        • RRB JE Online Coaching
        • RRB JE Electronics Coaching
        • RRB ALP Exam Coaching
        • RRB Technician Exam Coaching
      • CAT Online Coaching
      • CMAT Online Coaching
      • XAT Exam Coaching
      • GATE Online Coaching
        • GATE CE Online Coaching
        • GATE ME Online Coaching
        • GATE EE Online Coaching
      • NMAT Exam Coaching
        • AAI ATC Jr Executive Coaching
      • Cochin Shipyard Executive Trainee Coaching
      • BEL Probationary Engineer Coaching
      • HPCL Junior Executive Coaching

    © 2023 Entri.app - Privacy Policy | Terms of Service

    Chat