Abstraction in Java – OOPS Concepts

In this post we’ll see the usage of Abstraction in Java.

What is abstraction

Abstraction is one of the four fundamental principles of Object Oriented Programming along with inheritance, polymorphism and encapsulation.

Abstraction means hiding the complexity of the implementation and just exposing the essential features to the user. As an example you can take any electronics item where you interact with the product using buttons and switches to turn it on and off or increase and decrease the volume or speed. The real complexity, how that functionality is implemented is hidden from us.

There are many APIs in Java that can be shown as an example of abstraction in Java. A very well known one such API is JDBC which is used to connect to DB, execute queries and fetch results. As a developer we just know the methods of the API which are uniform, how these methods are implemented for different DBs is abstracted from us. We just add the required Driver for the used DB and use the same methods.

Abstraction in Java

If we have to hide the real implementation from the user that kind of abstraction in Java can be achieved through interfaces and abstract classes. By using interface the implementation is completely hidden, only the expected behavior is defined. With abstract class partial implementation can be provided.

Abstraction in Java – Interface example

Consider the following interface-

Only the expected behavior is defined through a method in this interface, how it is implemented is abstracted.

There are two classes implementing this interface and providing method implementation as per type of User.

You can run the implementation using the following class.

Output

In the class using the User instance you can run the required implementation by just switching the reference. How those User implementing classes are implemented is abstracted.

Abstraction in Java – Abstract class example

If we take the previous example there can be some common methods irrespective of the type of the user like getting name of the user, getting date of birth of the user where as calculation of the fee may differ.

In this scenario we can use an Abstract class keeping the common implementation there itself and abstracting the part that differ.

In the abstract class processFee() method is abstract so it will be implemented by the classes extending the User class.

PlatinumUser.java

GoldUser.java

You can run the implementation using the following class.

Output

In the class using the User instance you can run the required implementation by just switching the reference. For the methods getDesignation() and getName() common implementation from the User class is used. For the processFee() method which is abstracted, implementation from the extending classes is used.

That’s all for the topic Abstraction in Java – OOPS Concepts. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.