What is the difference between Encapsulation and Abstraction?

By | March 7, 2023

In Java, encapsulation and abstraction are two important concepts in object-oriented programming. Here’s the difference between them:

Encapsulation:

Encapsulation is the concept of hiding the implementation details of an object and exposing only the necessary information to the outside world. It is achieved by using access modifiers such as private, protected, and public to control access to the internal state of an object.

For example, consider a class Person with private instance variables name, age, and email. We can use public methods such as getName(), getAge(), and getEmail() to allow external code to access these variables, while keeping the implementation details hidden:

public class Person {
private String name;
private int age;
private String email;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}

In this example, the name, age, and email variables are private, so they cannot be accessed directly from outside the class. Instead, we provide public getter and setter methods to allow external code to access and modify these variables.

Abstraction:

Abstraction is the concept of representing complex real-world objects using simplified models in the code. It involves identifying the essential features of an object and ignoring the non-essential ones.

For example, consider an abstract class Shape with an abstract method getArea(). We can create concrete classes such as Circle, Rectangle, and Triangle that extend the Shape class and provide their own implementation of getArea(). This allows us to represent different shapes using a common interface:

public abstract class Shape {
public abstract double getArea();
}

public class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}

public double getArea() {
return Math.PI * radius * radius;
}
}

public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getArea() {
return length * width;
}
}

In this example, the Shape class represents a simplified model of a shape, with the getArea() method representing the essential feature of a shape. The Circle and Rectangle classes extend the Shape class and provide their own implementation of getArea(), representing more specific shapes.

In summary, encapsulation is the concept of hiding implementation details, while abstraction is the concept of representing complex real-world objects using simplified models in the code. Encapsulation is achieved using access modifiers such as private, protected, and public, while abstraction is achieved using abstract classes and methods.

Leave a Reply

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