1. Introduction
Encapsulation in Java is the technique of bundling the data (variables) and code acting on the data (methods) together as a single unit. It restricts direct access to some of an object's components, which can prevent the accidental modification of data. Encapsulation is achieved using access modifiers, allowing you to control the visibility of class members.
Abstraction in Java is a concept that aims to hide the complex reality while exposing only the necessary parts. It is achieved using abstract classes and interfaces. Abstraction lets you focus on what an object does instead of how it does it, facilitating a higher level of program complexity management. It serves as a foundation for modelling complex systems by simplifying object interactions.
2. Key Points
1. Encapsulation hides the internal state and requires all interaction through an object's methods.
2. Abstraction hides the implementation details and only shows the user the necessary functionality.
3. Encapsulation is achieved in Java using private, protected, and public access modifiers.
4. Abstraction is achieved in Java using abstract classes and interfaces.
3. Differences
Encapsulation | Abstraction |
---|---|
The process of wrapping code and data into a single unit, such as a Java class. | The concept of hiding the complex implementation details and showing only the necessary features of an object. |
This is achieved in Java by restricting access to class members using access modifiers like private, public, protected, and default. | Achieved in Java using abstract classes and interfaces. |
Encapsulation is used to protect a class's data (variables) from unauthorized access and modification. | Abstraction is used to hide the complex implementation details and show only the necessary functionalities to the user. |
It focuses on controlling access to data and the methods of an object to ensure integrity and security. | It focuses on reducing complexity by hiding details and exposing only the essential parts of a system. |
In encapsulation, data hiding is achieved by making a class's variables private and providing public getter and setter methods to modify and view the variables' values. | In abstraction, complexity is managed by providing abstract interfaces that represent complex entities more straightforwardly. |
Example: A capsule tightly encapsulates several combinations of medicines. | Example: A TV remote abstracts the complex electronics inside the TV and provides the user with a simple interface. |
4. Example
// Example of Encapsulation
public class Employee {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
// Example of Abstraction
public abstract class Animal {
public abstract void makeSound();
public void eat() {
System.out.println("Animal is eating");
}
}
public class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Woof woof");
}
}
Explanation:
1. The Employee class uses encapsulation by keeping its name field private and providing public getter and setter methods.
2. The Animal class demonstrates abstraction by providing a method eat that has an implementation and an abstract method makeSound that does not have an implementation.
Comments
Post a Comment
Leave Comment