Popular Posts

July 11, 2024

Difference between Abstraction and Encapsulation in Java

 

In Java, abstraction and encapsulation are two fundamental object-oriented programming concepts that help in building robust, reusable, and maintainable code. Though they are related, they serve different purposes.

Abstraction

Definition: Abstraction is the concept of hiding the complex implementation details and showing only the essential features of the object. It allows you to focus on what the object does instead of how it does it.

Purpose:

  • To reduce complexity.
  • To increase the efficiency of the design.

How to Achieve:

  • By using abstract classes and interfaces.

Example:

// Abstract Class

abstract class Animal {

    // Abstract method (does not have a body)

    abstract void makeSound();

    

    // Regular method

    void eat() {

        System.out.println("This animal eats food.");

    }

}


// Concrete Class

class Dog extends Animal {

    @Override

    void makeSound() {

        System.out.println("The dog says: Woof Woof");

    }

}


// Interface

interface Vehicle {

    void start();

    void stop();

}


// Implementing the interface

class Car implements Vehicle {

    @Override

    public void start() {

        System.out.println("The car starts.");

    }


    @Override

    public void stop() {

        System.out.println("The car stops.");

    }

}


In this example, Animal is an abstract class with an abstract method makeSound. The Dog class provides the specific implementation of this method. Similarly, the Vehicle interface declares the methods start and stop, and the Car class implements these methods.

Encapsulation

Definition: Encapsulation is the mechanism of wrapping the data (variables) and code (methods) together as a single unit. It also restricts direct access to some of an object's components, which can prevent the accidental modification of data.

Purpose:

  • To protect the internal state of an object.
  • To achieve modularity.
  • To reduce the complexity of the code.

How to Achieve:

  • By declaring the variables of a class as private and providing public getter and setter methods to modify and view the values of the variables.

Example:

public class Person {

    // Private variables

    private String name;

    private int age;


    // Getter method for name

    public String getName() {

        return name;

    }


    // Setter method for name

    public void setName(String name) {

        this.name = name;

    }


    // Getter method for age

    public int getAge() {

        return age;

    }


    // Setter method for age

    public void setAge(int age) {

        if(age > 0) {

            this.age = age;

        } else {

            System.out.println("Age must be positive.");

        }

    }

}


public class Main {

    public static void main(String[] args) {

        // Creating an object of Person class

        Person person = new Person();

        

        // Setting values through setter methods

        person.setName("John");

        person.setAge(25);


        // Accessing values through getter methods

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

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

    }

}


In this example, the Person class encapsulates its data members (name and age) by making them private. The class provides public getter and setter methods to access and modify these variables, ensuring that the internal state of the object is protected and can only be changed in controlled ways.

Summary

  • Abstraction focuses on hiding the complex implementation details and exposing only the necessary parts. It is achieved using abstract classes and interfaces.
  • Encapsulation focuses on bundling the data and methods that operate on the data within one unit and restricting direct access to some of the object's components. It is achieved using access modifiers like private, protected, and public along with getter and setter methods.

No comments:
Write comments