Abstraction is the technique in which the programmer hides, a part of code for view and modification. This code is not available to the user as such but he can implement it wherever he likes. The freedom of implementation of a particular piece of code without essentially having to share the code is called abstraction.

In abstraction we can’t initiate this class and we can use this class and methods only via extends with another class.

Encapsulation: Encapsulation means to create a packet/module/class containing methods and data. The process of bundling data and thereby restricting access to some of its components while allowing access to others is called encapsulation.

Abstraction means to define methods whose purpose is to be extended/implemented only. Encapsulation is hiding what is not needed by the user. Abstraction separates the users "view" from the implementation.

Abstraction - hiding implementation.

Encapsulation - hiding data.


public class Student extends College {


     public static void main(String args[]) {

           // Constructor of current class

           Student student = new Student();


           // Constructor of encapsulation class

           StudentInfo info = new StudentInfo();

           // set and get from encapsulated methods


           info.setStudentName("Zack Anderson");

           info.setStudentAddress("10B mg road Mumbai, India");


 System.out.println("College Code    -: " + student.collegeCode());

 System.out.println("College Name    -: " + student.collegeName());

 System.out.println("Student Id      -: " + info.getStudentId());

 System.out.println("Student Name    -: " + info.getStudentName());

System.out.println("Student Address -: " + info.getStudentAddress());

 System.out.println("Student Contact -: " + info.gerStudentContact());



     // Override abstract methods


     public int collegeCode() {

           return 101;




     public String collegeName() {

           return "MyCollege Name";




  // Abstract class

  abstract class College {

     // define abstract methods

     public abstract int collegeCode();


     public abstract String collegeName();




  // Encapsulation methods

  class StudentInfo {

     private int studentId;

     private String studentName;

     private String saddress;

     private String contactno;


     // use getter and setter methods

     public void setStudentId(int id) {

           studentId = id;



     public int getStudentId() {

           return studentId;



     public void setStudentName(String name) {

           studentName = name;



     public String getStudentName() {

           return studentName;



     public void setStudentAddress(String address) {

           saddress = address;



     public String getStudentAddress() {

           return saddress;



     public void setStudentContact(String contact) {

           contactno = contact;



     public String gerStudentContact() {

           return contactno;





Run your application

Difference between Abstraction and Encapsulation in Java


  Modified On Dec-05-2017 12:18:06 AM

Leave Comment