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.
0 Comments