Difference between Abstraction and Encapsulation is that,
Abstraction is implemented in Java using interface
and abstract class while Encapsulation is implemented using private,
package-private and protected access modifier.
Encapsulation is also called data hiding.
Encapsulation:
Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction.
Encapsulation
in Java is a mechanism for wrapping the data (variables) and code
acting on the data (methods) together as a single unit. In
encapsulation, the variables of a class will be hidden from other
classes and can be accessed only through the methods of their current
class. Therefore, it is also known as data hiding.
To achieve encapsulation in Java −
● Declare the variables of a class as private.
● Provide public setter and getter methods to modify and view the variables values.
Abstraction:
Likewise in Object-oriented programming, abstraction is a process of
hiding the implementation details from the user, only the functionality
will be provided to the user. In other words, the user will have the
information on what the object does instead of how it does it.
In Java, abstraction is achieved using Abstract classes and interfaces.
No comments:
Post a Comment