Abstraction in Object Oriented Programming
Abstraction enables us to provide a simple interface to a user without asking for complex details to perform an action. In simpler words, it means giving the user the ability to drive the car without the need to understand tiny details of ‘how does the engine work’. An end-user using the application need not be concerned about how a particular feature is implemented. He/she can just use the features as required. Thereby the user will only know “what it does” rather than “how it does”.
Note: Examples of this and its related articles are related to high level statically typed object oriented languages such as C# and Java.
Types of abstraction:
- Data Abstraction
- In data abstraction, we mostly create complex data types and hide their implementation. We only expose the operations to manipulate these data types without going into the details of their implementation. One advantage of this approach is that we can change the implementation anytime without changing the behaviour that is exposed to the user.
- Control Abstraction
- Control abstraction collects all the control statements that are a part of the application and exposes them as a unit. This feature is used when we have to perform a working feature using this control unit. Control abstraction forms the main unit of structured programming and using control abstraction we can define simple functions to complex frameworks.
How many ways can we implement Abstraction?
Abstraction or “an abstraction” can range from a simple constant to a set of interfaces, e.g. an entire API. This range also includes a single function, and also a group of methods (i.e. a class or interface). The quality of an abstraction goes to its usability and completeness: an incomplete abstraction often requires clients to know more than they should about its implementation detail to make up the short fall, which creates tighter coupled code than we’d like to see. So, for example, a single constant, while a form of abstraction, is probably not very complete on its own without other constants and methods.
Implement — Using abstract class and interface
Abstraction is implemented using an abstract class and interface. We can use abstract keyword on classes and methods. The interface provides complete abstraction i.e. it only provides method prototypes and not their implementation. An abstract class provides partial abstraction wherein at least one method should not be implemented.
Example — using abstract class:
Example — using interface:
Implement — Layering
Layering is the alternation of providing an abstraction, whose implementation is in terms of other abstractions. Ideally, the consumer of a given layer (e.g. the layer above) interacts only with that layer and does not need to go to a lower layer.
Implement — Using Encapsulation
A simple way of doing it is to just use private members. GetActiveServers() could call no private methods, or 5 private methods. That doesn’t really matter when we’re using it, so long as it works efficiently as needed.
- Classes and interfaces are a formal mechanism that groups methods into an abstraction.
- Namespaces are a formal mechanism that groups classes and interfaces (and other namespaces) into an abstraction of a larger API.
Thank you for reading this article. I hope this and its related articles will help you start your programming journey.
Can a class achieve abstraction without implementing an interface or extending an abstract class?
Abstraction, or "an abstraction" can range from a simple constant to a set of interfaces, e.g. an entire API. This…
Disclaimer & fair use statement:
This website may contain copyrighted material, the use of which may not have been specifically authorized by the copyright owner. This material is available in an effort to explain concept of Object Oriented Programming designs in an articulate and summarized manner to be used as an educational tool. The material contained in this website is distributed without profit for research and educational purposes. Only small portions of the original work are being used and those could not be used easily to duplicate the original work. This should constitute a ‘fair use’ of any such copyrighted material (referenced and provided for in section 107 of the US Copyright Law). If you wish to use any copyrighted material from this site for purposes of your own that go beyond ‘fair use’, you must obtain expressed permission from the copyright owner.