Encapsulation in java with example

Encapsulation in java is the process of binding related data(variables) and functionality(methods) into a single unit called class. Encapsulation can be achieved by using access modifier such as public, private, protected or default, so your class will be safe from unauthorized access by others and will be simple to maintain.

We can create fully encapsulated class by

  • Making variables private
  • Providing getters and setters methods for the accessing the variables.

Encapsulation is also termed as data hiding because you are making variables private and variables can be only accessed through public getters and setters.

Example of Encapsulation in java:

When you run above program, you will get below output:

Employee Id: 1
Employee Name: John
Employee Department: Sales

Advantages of Encapsulation in java:

  • It provides control over data. For example: If You want to check if age of employee is greater than 18 in setter method(setAge(int age)). You can easily do it in setter method without breaking any code.
  • Increase reusability.
  • Makes class easy to use for other clients.
  • It helps the developer to write code more flexible and maintainable by binding them into a single unit and use appropriate access modifier to access the code as per need.

Difference between abstraction and encapsulation in java:

  • Encapsulation means data hiding using getter and setters. Abstraction means hiding implementation details using [abstract class](https://java2blog.com/abstract-class-java/ “abstract class”) and [interface](https://java2blog.com/interface-in-java-with-example/ “interface”).
  • Abstraction is more of design level concept and Encapsulation is more of implementation level concept.

That’s all about encapsulation in java.

Was this post helpful?

Related Posts

  • 30 January

    Difference between early binding and late binding in java

    Table of ContentsEarly Binding or Static BindingWhy are static final and private methods associated by Early Binding?Code Example:Analysis:Late Binding or Dynamic BindingCode Example:Output:Analysis:Differences between Static and Dynamic Binding Binding in Java refers to the process of associating a method or function body with a method or function call by the Java Compiler. In simpler words, […]

  • 04 November

    Can we overload main method in java

    In this post, we will see about "Can we overload main method in java".This is one of the most asked Core java interview questions. Yes, we can overload main method in java but when you run your program, JVM will search for public static void main(String[] args) and execute that method. Overload main method in […]

  • 02 November

    Dynamic method dispatch in java

    In this post, we will about Dynamic method dispatch which is also referred as run time polymorphism. Dynamic Method Dispatch Dynamic method dispatch is a technique by which call to a overridden method is resolved at runtime, rather than compile time.When an overridden method is called by a reference, then which version of overridden method […]

  • 29 October

    Can we override static method in java

    No, we can not override static method in java. Static methods are those which can be called without creating object of class,they are class level methods. On other hand,If subclass is having same method signature as base class then it is known as method overriding. Its execution decided at run time. Below are the reasons why […]

  • 26 August

    Copy Constructor in java

    In this post, we will see about copy constructor in java. Copy constructor is the constructor which takes parameter as object of same class and copy each field of the class to the new object. Unlike C++, java does not provide default copy contructor. You need to create one, if you want to have copy […]

  • 26 August

    Java default constructor

    In this post, we will see about Java default constructor. Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly. You won’t able to see it as it is present in class file rather than source file. Is there any difference between no argument constructor and […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.