What Is Abstraction?

Definition

Abstraction is one of the principle of object oriented programming. It is used to display only necessary and essential features of an object to ouside the world.Means displaying what is necessary and encapsulate the unnecessary things to outside the world.Hiding can be achieved by using "private" access modifiers.

Note - Outside the world means when we use reference of object then it will show only necessary methods and properties and hide methods which are not necessary.


Implementation of Abstraction

To implement abstraction let's take an example of a car. We knows a car, Car is made of name of car, color of car, steering, gear, rear view mirror, brakes, silencer, exhaust system, diesal engine, car battery, car engine and other internal machine details etc.

Now lets think in terms of Car rider or a person who is riding a car. So to drive a car what a car rider should know from above category before he starts a car driving.

Necessary things means compulsary to know before starting a car

1. Name of Car
2. Color of Car
3. Steering
4. Rear View Mirror
5. Brakes
6. Gear

Unnecessary things means  not that compulsary to know for a Car rider

1. Internal Details of a Car
2. Car Engine
3. Diesal Engine
4. Exhaust System
5. Silencer


Now above same thing let me put in the coding style using C#
01public class Car
02 
03    private string _nameofcar = "My Car";
04    private string _colorofcar = "Red";
05 
06    public string NameofCar
07     
08        set
09         
10            _nameofcar = value;
11        }
12        get
13         
14            return _nameofcar;
15        }
16    }
17 
18    public string ColorofCar
19     
20        set
21         
22            _colorofcar = value;
23        }
24        get
25         
26            return _colorofcar;
27        }
28    }
29 
30    public void Steering()
31     
32        Console.WriteLine("Streering of Car");
33    }
34 
35    public void RearViewMirror()
36     
37        Console.WriteLine("RearViewMirror of Car");
38    }
39 
40    public void Brakes()
41     
42        Console.WriteLine("Brakes of Car");
43    }
44    public void Gear()
45     
46        Console.WriteLine("Gear of Car");
47    }
48 
49 
50    private void InternalDetailsofCar()
51     
52        Console.WriteLine("InternalDetailsofCar of Car");
53    }
54 
55    private void CarEngine()
56     
57        Console.WriteLine("CarEngine of Car");
58    }
59 
60    private void DiesalEngine()
61     
62        Console.WriteLine("DiesalEngine of Car");
63    }
64 
65    private void ExhaustSystem()
66     
67        Console.WriteLine("ExhaustSystem of Car");
68    }
69 
70    private void Silencer()
71     
72        Console.WriteLine("Silencer of Car");
73    }
74 
75 
76}

As you can see from above code that necessary methods and properties exposed by using "public" access modifier and unnecessary methods and properties (not compulsary) hidden by using "private" access modifier.

As you see to achieve abstraction we used access modifier "public" to expose some methods and properties to outside the class or world.

As you see to achieve abstraction we used access modifier "private" to hide some methods and properties from outside the class or world.
Finally lets check by creating an object of above class "Car" in our main program of a console application and by using object lets see weather we are getting all exposed necessary methods and properties.

1class Program
2 
3    static void Main(string[] args)
4     
5        Car objCar = new Car();
6 
7    }
8}

Conclusion
Sucessfully we have exposed necessary methods and properties to outside the world or class. This is how we need to implement abstraction or we can achieve abstraction in our code or application.

Comments

Popular posts from this blog