Java Oops - Abstraction in Java

 Abstraction in Java :

1. Abstraction means Data hiding, in another word we can say that in this type of programming essential data is shown to the user or outside class and unessential data is hidden.

2. Members defined with a public access specifier are accessible throughout the program.

3. Members defined with a private access specifier are not accessible throughout the program means a private element of a class can be accessed only inside in its own class.



Example :

Let's take a real-life example assume that you are going to buy a car in a showroom then you can know about the company name, model name, color, cost, and oil type but you don't know about piston and its functionality, the person who made that model of car.

 For better understanding see the below example:

1:  class Showroom  
2:  {  
3:  public void company()  
4:  {  
5:   System.out.println("Renault");   
6:  }  
7:  public void model()  
8:  {  
9:   System.out.println("Duster");   
10:  }  
11:  public void color()  
12:  {  
13:   System.out.println("White/Gray/Silver/Black/Brown");   
14:  }  
15:  public void oilType()  
16:  {  
17:   System.out.println("Petrol");   
18:  }  
19:  public void price()  
20:  {  
21:   System.out.println("800,000 to 1400,000");   
22:  }  
23:  //private member  
24:  private void piston()  
25:  {  
26:   System.out.println("4 piston");   
27:  }  
28:  //private member  
29:  private void person_who_made()  
30:  {  
31:   System.out.println("Alexo Remon");   
32:  }  
33:  }  
34:  class Easy  
35:  {  
36:   public static void main(String[] args)   
37:   {  
38:   //creating instance(object) of class  
39:   Showroom obj=new Showroom();  
40:   //calling function  
41:   obj.company();  
42:   obj.model();  
43:   obj.color();  
44:   obj.price();  
45:   obj.oilType();  
46:   }  
47:  }  
48:  /*  
49:  ### Output ###  
50:  Renault  
51:  Duster  
52:  White/Gray/Silver/Black/Brown  
53:  800,000 to 1400,000  
54:  Petrol  
55:  */  


Advantage of Data Abstraction:

1. We can provide security of data using Abstraction.

2. Data Abstraction avoids code duplication and increases code reusability.

3. We don't have to write the low-level code because the private element of a class can not be accessed outside that class.