top of page

Difference between Abstraction and Encapsulation in Java

Java is an object-oriented programming language and it follows OOPs concepts. The OOPs concepts include classes, objects, polymorphism, inheritance. There are two other features of OOPs i.e. abstraction and encapsulation. They both seem very similar but totally different in concept and implementation. The major difference between abstraction and encapsulation is that abstraction hides the code complexity while encapsulation hides the internal working from the outside world. In this section, we will discuss abstraction and encapsulation and the differences between abstraction and encapsulation in Java.


Abstraction

It is a feature of OOPs. It is used to hide the unnecessary information or data from the user but shows the essential data that is useful for the user. It can be achieved by using the interface and the abstract class. In interfaces, only the methods are exposed to the end-user. The best example of abstraction is a TV remote. The user only interacts with the outer interface that is nothing but keys. The user only knows which key to press for what function.


Let's understand the abstraction through a Java program.

//abstract class 
abstract class Shape   
{  
//abstract method 
//note that we have not implemented the functionality of the method 
public abstract void draw();  
}  
class Circle extends Shape  
{  
//implementing functionality of the abstract method 
public void draw()   
{  
System.out.println("Circle!");  
}  
}  
//main class  
public class Test   
{  
public static void main(String[] args)   
{  
Shape circle = new Circle();  
//invoking abstract method draw() 
circle.draw();  
}  
}  


Encapsulation

It is also a feature of OOP. It is used to bind up the data into a single unit called class. It provides the mechanism which is known as data hiding. It is an important feature of OOPs. It prevents to access data members from the outside of the class. It is also necessary from the security point of view.

Let's understand the abstraction through a Java program.


EncapsulationDemo.java

//A Java class to test the encapsulated class Account   
public class EncapsulationDemo  
{    
public static void main(String[] args)   
{    
//creating instance of Account class   
Account acc=new Account();    
//setting values through setter methods   
acc.setAcc_no(7560504000L);    
acc.setName("Mark Dennis");    
acc.setEmail("md123@gmail.com");    
acc.setAmount(500000f);    
//getting values through getter methods   
System.out.println(acc.getAcc_no()+" "+acc.getName()+" "+acc.getEmail()+" "+acc.getAmount());    
}    
}   


Account.java

class Customer  
{    
//private data members   
private long cstmr_id;    
private String cstmr_name, cstmr_email;    
private float amount;    
//public getter and setter methods   
public long getAcc_no()   
{    
return acc_no;    
}    
public void setAcc_no(long acc_no)   
{    
this.acc_no = acc_no;    
}    
public String getName()   
{    
return name;    
}    
public void setName(String name)   
{    
this.name = name;    
}    
public String getEmail()   
{    
return email;    
}    
public void setEmail(String email)   
{    
this.email = email;    
}    
public float getAmount()   
{    
return amount;    
}    
public void setAmount(float amount)   
{    
this.amount = amount;    
}      
}    


Difference Between Abstraction and Encapsulation




Source: Java67


The Tech Platform

0 comments
bottom of page