Abstraction in Java - OOPS Concept Explained

Abstraction is one of the fundamental Object Oriented Programming concepts. It is a mechanism to show only essential data and hide the internal implementation of software, this is known as method hiding.

Why need Abstraction?

Object Oriented Programming helps to manage the complexity of understanding things with the help of abstraction. A client doesn't need to know how the car engine works when it starts/stops, all they care is ease of use. So the actual Engineer hides all the internal engine mechanisms and provides the essential info(a simple key mechanism to start/stop the car). Similarly in the programming world a software developer hides all the implementation of a car engine and provides only the essential info to start the car.






One more example, Let's consider a head bank class that contains taxCalculation() for its child bank in different countries, In this scenario developer can't provide a direct implementation in a single class so they can create an abstract class(the idea about HeadBank operations) that shows only taxCalculation() available for tax calculating. When a client uses the HeadBank class they may know taxCalculation() provides the amount of tax to be paid as a result but not the calculation process step by step.

With the help of inheritance, this approach helps the engineers to hide the internal details and show only the essential information. In the future, if taxCalucation() process needs to be updated due to tax percentage increase or money inflation then the engineer works on the hidden internal details and the client still receives the proper essential information(tax to be paid result) without knowing the internal details.

How to achieve abstraction?

  • Create a abstract class or interface
  • Create a abstract method (declare a method)
  • Create a class that inherits abstract class or interface
  • Provide the implementation to abstract method






Post a Comment

Previous Post Next Post
B