Example & Tutorial understanding programming in easy ways.

8.What is the difference between abstraction and encapsulation?

 

                                        encapsulation

1.Encapsulation is a process of hiding all the data and methods within a class from outside world

2.Encapsulation in Java is represented using private, package-private and protected access modifier. It wraps the variables, method data and codes within a class into a single entity. The class and methods outside the wrapper cannot access the code and data defined under the wrapper. In encapsulation object becomes a container (or capsule) for related data variables and methods.

3.Encapsulation solves the problem in implementation level

                                                   abstraction


1.Abstraction on the other hand displays the essential features but hide the unnecessary features of an object.

2.Abstraction in Java is represented by Interface, Abstract class, Abstract methods using "abstract" keyword. It hides certain details of the object that are not important and display that are essential. In deals with the outside view of an object (interface).


3.while the Abstraction solves the problem in the designing level.
 


Following is the example of Encapsulation :-
 

public class table

{

private int width;

private int height;

private int length;

public void setLength(int p)

{

length = p;

}

public void setWidth(int p)

{

width = p;

}

public void setHeight(int p)

{height = p;}

public int displayVolume()

{

System.out.println(length*width*height)...

}

public static void main(String args [ ])

{

table b1=new table(4,5,6);

b1.displayvolume();

}

 


Following is the example of Abstraction:
 

abstract class A

{

public abstract abs value();

void show()

{

System.out.println("This is an abstract class");

}

}

 

Read More →