Data Abstraction in Java

Hello friends how are you, today in this blog i will teach you what is Data Abstraction, how to use data abstraction in program, advantage of data abstraction, how data abstraction works and many programs using data abstraction in a very simple way. 

Let's start

1.Abstraction means Data hiding ,in other 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 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 showroom then you can know about 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 .

class Showroom
{
public void company()
{
 System.out.println("Renault"); 
}
public void model()
{
 System.out.println("Duster"); 
}
public void color()
{
 System.out.println("White/Gray/Silver/Black/Brown"); 
}
public void oilType()
{
 System.out.println("Petrol"); 
}
public void price()
{
 System.out.println("800,000 to 1400,000"); 
}
//private member
private void piston()
{
 System.out.println("4 piston"); 
}
//private member
private void person_who_made()
{
 System.out.println("Alexo Remon"); 
}
}
class Easy
{
 public static void main(String[] args) 
 {
  //creating instance(object) of class
 Showroom obj=new Showroom();
 //calling function
 obj.company();
 obj.model();
 obj.color();
 obj.price();
 obj.oilType();
 }
}
/*
### Output ###
Renault
Duster
White/Gray/Silver/Black/Brown
800,000 to 1400,000
Petrol
*/

Advantage of Data Abstraction

1.We can provide security of data using Abstraction.
2.Data Abstraction avoids code duplication and increases the code reusability.
3.We don't have to write the low-level code because private element of a class can not be accessed outside that class.


Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. 

Try to learn something new and teach something new to other. Thanks.😊

Post a Comment

0 Comments