Java - Difference Between Abstraction and Encapsulation

By Shahnail Khan Last updated : March 14, 2024

In Java programming, two fundamental concepts, abstraction and encapsulation, play a major role in designing robust and maintainable code. While they both contribute to achieving modular and organized code, they serve different purposes. In this article, we’ll understand the difference between abstraction and encapsulation in Java in detail.

What is Abstraction in Java?

Abstraction is the process of hiding the irrelevant details and showing only the essential features of an object. It enables developers to focus on what an object does rather than how it achieves its functionality. In Java, abstraction is typically achieved through abstract classes and interfaces.

Example of Abstraction in Java

abstract class Animal {
  abstract void makeSound();

class Dog extends Animal {
  void makeSound() {

class Cat extends Animal {
  void makeSound() {

public class Main {
  public static void main(String[] args) {
    Animal dog = new Dog();
    Animal cat = new Cat();

    dog.makeSound(); // Output: Woof!
    cat.makeSound(); // Output: Meow!

The output of the above program is:



  • We have an abstract class called Animal.
  • The Animal class has an abstract method called makeSound(), indicating that every animal must have a sound, but the specific sound is not defined in the abstract class.
  • Two concrete subclasses of Animal are defined: Dog and Cat.
  • These subclasses provide specific implementations for the abstract method makeSound().
  • Dog class implements makeSound() with "Woof!", and the Cat class implements it with "Meow!".
  • In the Main class, we create instances of Dog and Cat, which are treated as Animal objects due to polymorphism.
  • We call the makeSound() method on each object.
  • Despite makeSound() being abstract in the Animal class, it's invoked successfully for Dog and Cat objects because they provide concrete implementations.
  • When we call makeSound() on the Dog object, it prints "Woof!".When we call makeSound() on the Cat object, it prints "Meow!".

What is Encapsulation in Java?

Encapsulation is like putting things in a box and only allowing certain ways to interact with them. In programming, it means bundling data (variables) and methods (functions) that work with the data together in a single unit, often called a class. This helps in keeping the data safe from outside interference and ensures that it is accessed and modified in controlled ways. Encapsulation helps in organizing code, improving security, and making it easier to manage and modify in the future.

Example of Encapsulation in Java

class Person {
  private String name;
  private int age;

  // Getter method for name
  public String getName() {
    return name;

  // Setter method for name
  public void setName(String name) { = name;

  // Getter method for age
  public int getAge() {
    return age;

  // Setter method for age with validation
  public void setAge(int age) {
    if (age >= 0) {
      this.age = age;
    } else {
      System.out.println("Age cannot be negative.");

public class Main {
  public static void main(String[] args) {
    Person person = new Person();

    System.out.println("Name: " + person.getName());
    System.out.println("Age: " + person.getAge());

    // Attempting to set a negative age

The output of the above program is:

Name: Rohan
Age: 30
Age cannot be negative.


  • The Person class has two private instance variables: name and age. These variables are not directly accessible from outside the class.
  • To access and modify the private variables, the Person class provides getter and setter methods:
  • getName() and setName(): These methods allow getting and setting the name variable, respectively.
  • getAge() and setAge(): These methods allow getting and setting the age variable, respectively. Additionally, the setAge() method includes a validation check to ensure that the age cannot be set to a negative value.
  • By encapsulating the name and age variables and providing controlled access through getter and setter methods, the Person class ensures that the internal state of a Person object is protected.
  • In the Main class, we create a Person object and use its setter and getter methods to set and retrieve the name and age values. 

Difference Between Abstraction and Encapsulation in Java

The below table shows the difference between abstraction and encapsulation in Java:

Sr. No. Java Abstraction Java Encapsulation
1 Focuses on the outside view of an object, hiding the implementation details Focuses on bundling data and methods into a single unit, hiding internal state
2 Deals with the behaviour of an object. Deals with data representation and data manipulation.
3 Achieved using abstract classes and interfaces. Achieved using access modifiers (e.g., private, public) and getter/setter methods.
4 Enables developers to define contracts/interfaces for objects. Ensures data security and controls access to the internal state of an object.

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.