- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between abstraction and encapsulation in Java?
As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of e-mail, complex details such as what happens as soon as you send an e-mail, the protocol your e-mail server uses are hidden from the user. Therefore, to send an e-mail you just need to type the content, mention the address of the receiver, and click send.
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.
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit.
Example
public class EncapTest { private String name; private String idNum; private int age; public int getAge() { return age; } public String getName() { return name; } public String getIdNum() { return idNum; } public void setAge( int newAge) { age = newAge; } public void setName(String newName) { name = newName; } public void setIdNum( String newId) { idNum = newId; } } public class RunEncap { public static void main(String args[]) { EncapTest encap = new EncapTest(); encap.setName("James"); encap.setAge(20); encap.setIdNum("12343ms"); System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge()); } }
Output
Name : James Age : 20
- Related Articles
- Difference Between Abstraction and Encapsulation
- Explain difference between Abstraction and Encapsulation in PHP.
- Abstraction vs Encapsulation in Java
- Difference between Abstraction and Virtualization
- Difference Between Abstraction and Data Hiding
- Difference Between Data Hiding and Encapsulation
- What is encapsulation in Java?
- What is an abstraction in Java?
- What is the difference between Java and Core Java?
- What is the difference between Java and Java EE
- What is the difference between /* */ and /** */ comments in Java?
- What is the difference between >> and >>> operators in Java?
- What is the difference between Java and JavaScript?
- Abstraction in Java
- What is the difference between compositions and aggregations in Java?
