Tuesday, December 10, 2013

Difference between Abstraction and Encapsulation


In Java both Abstraction and Encapsulation are two important OOP (Object Oriented Programming) concepts or principles. Both are completely different from each other.
 
Abstraction(Hiding): Abstraction means hiding.

Abstraction is the process of hiding the details and exposing only the essential features of a particular concept or object.

For example, while making JDBC connection, Connection interface refer to an instance of a class which is implemented by any of the third party vendor, so we don't know what is the implementation details of the driver.

Another example is, when we use String.substring() method, we just have to worry about using the substring() method according to its specifications. We should not be bothered about or try to change the way the substring() method is defined in the String class.

Abstraction can be implemented by using Interface, Abstract class, Abstract methods using "abstract" keyword. It hides certain details of the object that are not important and display that are essential.

Abstraction focuses on the outside view of an object (i.e. the interface).

Advantages:
1. Code will be clean and for managing it will be very easy
2. Data can be accessed by member functions of class only and any external function can't manipulate the data. The data is more secure.

Example:

package com.ranga;

abstract class Student {
  private long studentNo;
  private String name;
  public abstract int totalMarks();

  protected Student(long studentNo, String name) {
    this.studentNo = studentNo;
    this.name = name;
  }

  public long getStudentNo() {
    return studentNo;
  }

  public void setStudentNo(long studentNo) {
    this.studentNo = studentNo;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Override
  public String toString() {
    return "Student{" +
        "studentNo=" + studentNo +
        ", name='" + name + '\'' +
        '}';
  }
}

class StudentImpl extends Student {
  private int marks1,marks2;
  @Override
  public int totalMarks() {
    return this.marks1+this.marks2;
  }
  StudentImpl(long studentNo, String name, int marks1, int marks2) {
    super(studentNo,name);
    this.marks1 = marks1;
    this.marks2 = marks2;
  }
}

public class AbstractionDemo {
  public static void main(String[] args) {
    Student student = new StudentImpl(100,"Ranga",50,60);
    System.out.println("Student Info: "+student);
    System.out.println("Student Total Marks: "+student.totalMarks());
    System.out.println("----------------------------------------");

    StudentImpl student1 = new StudentImpl(1043,"Ranga Reddy",60,40);
    System.out.println("Student Info: "+student1);
    System.out.println("Student Total Marks: "+student1.totalMarks());

  }
}

Output: 
Student Info: Student{studentNo=100, name='Ranga'}
Student Total Marks: 110
----------------------------------------
Student Info: Student{studentNo=1043, name='Ranga Reddy'}
Student Total Marks: 100

 

Encapsulation(Binds): Encapsulation means binding.

Encapsulation is a process of binding or wrapping the data and its associated members into a single unit. It is also called data hiding.

Class is the best example of encapsulation. 

Another example is Capsule.

Encapsulation can be implemented using private, package-private and protected access modifier.

Encapsulation(information hiding) prevents clients from seeing its inside view.

We can achieve complete encapsulation in java by making members of a class private and access them outside the class only through getters and setters.
Advantages:
  1. Maintenance will be good.  
  2. Encapsulation allows you to change one part of code without affecting other part of code.
  3. By using Encapsulation we can write immutable classes in Java.
Example:
EncapsulationDemo.java

package com.ranga;

class Student {

  // data members
  private long no;
  private String name;
  private int age;

  // member functions
  public long getNo() {
    return no;
  }

  public void setNo(long no) {
    this.no = no;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  @Override
  public String toString() {
    return "Student{" +
        "no=" + no +
        ", name='" + name + '\'' +
        ", age=" + age +
        '}';
  }
}

public class EncapsulationDemo
{
    public static void main( String[] args )
    {
      Student student = new Student();
      student.setNo(100);
      student.setName("Ranga");
      student.setAge(25);
      System.out.println(student);
    }
}


Output:
Student{no=100, name='Ranga', age=25}

Note: In the above Student class, we are making data members and it's associated members into a single component or single unit.

5 comments:

hitesh kumar said...

Difference between Encapsulation and Abstraction

thanks for this post

maha said...
This comment has been removed by the author.
maha said...

The post is totally awesome! Bunches of incredible information and motivation the two of which we as a whole need! Likewise prefer to appreciate the time and effort you put into your blog.

Digital Marketing Training in Chennai

Digital Marketing Course in Chennai

Sarika said...

Excellent blog I visit this blog it's really awesome. The important thing is that in this blog content written clearly and understandable. The content of information is very informative. We are also providing the best services click on below links to visit our website.
Oracle Fusion HCM Training
Workday Training
Okta Training
Palo Alto Training
Adobe Analytics Training

Techystick said...

what is azure
azure trial account
azure data factory
adf interview questions
azure certification path
azure traffic manager
cloudkeeda
cloudkeeda