Understating of Abstraction in Java

By | August 23, 2018

This tutorial talks about Understating of  Abstraction in Java.This is one of the important Oops feature in object oriented programming language.

Abstraction is one of the four fundamental Oops concepts. The other three are-

  1. Encapsulation
  2. Polymorphism
  3. Inheritance

Abstraction is a concept or an Idea which is not associated with any particular object or instance. Using abstract classes/
interfaces we can express the intent of the class rather than the actual implementation. In a way, one class should
not know the inner details or implementation of another in order to use it, just knowing the interfaces should be good more than enough.

We can take some real world example of abstraction like

A vehicle which we drive without  knowing what all is going underneath.
A TV set where we enjoy programs without knowing the inner details of how TV works.

An example of abstraction in Java would be

Java Database Connectivity (JDBC) API which provides universal database access from the Java programming language. Using the JDBC API, we can access virtually any data source without knowing how the driver for that particular data source is implemented. we have only an API with a given set of methods

Abstraction in Java

Abstraction in Java we can achieve in two ways –

  • Interface– Which defines an expected behavior without providing any details about the behavior.
  • Abstract Class– Which provides incomplete functionality and leave it on the extending class to fills the gaps.

 Let’s try to understand  how we can achieve Abstraction in Java   through Interface

We have an interface MyInterface.java 

You can see here, there are 2 methods in the interface MyInterface

printWelcomeMessage and getWelcomeMessage.

Here printWelcomeMessage method has one parameter of type String and does not return any value. Whereas, getWelcomeMessage method has one String parameter and returns a String value.

Let’s say we have two classes MyClassImpl1.java and MyClassImpl2.java implementing the interface MyInterface.


Here the printWelcomeMessage method is implemented to print the passed parameter. Method getWelcomeMessage prefixes Hello to the passed parameter.


Here the printWelcomeMessage method is implemented to print  the passed parameter in upper case. Method getWelcomeMessage  returns the passed parameter after converting it to upper case.


This client is going to use above created interface and both implementation classes to understand Abstraction in java

If you run ClientTest.java you will get below output:

In ClientTest class there is an object of type MyInterface when it has reference of type MyClassImpl1 it is calling methods of that class, when it has reference of type MyClassImpl2 it is calling methods of that class. But as a user you are abstracted from the different implementations.

Would you like to watch on YouTube:

That’s all about Understating of Abstraction in Java.

You May Also Like:

What are JVM, JRE and JDK in Java?
Differences between Java EE and Java SE
Encapsulation in Java
Understating of Polymorphism in Java
Method Overloading or Static Polymorphism in Java
Method Overriding or Dynamic Polymorphism in Java
Understating of Inheritance in Java

If you have any feedback or suggestion please feel free to drop in blow comment box.

Leave a Reply

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