Data Structures Algorithms & System Design(HLD+LLD)
by Logicmojo

Top tech companies experts provide live online training

Learn Data Structures, Algorithms & System Design

Online live classes from 4 to 7 months programs

Get job assistance after course completion

Download Course Brochure

Back to home
Logicmojo - Updated Dec 19, 2022


Java, as a programming language, follows almost all of the object oriented concepts. One of the main pillars of any object oriented language is "Abstraction". Java helps us implement this concept with the help of different interfaces and classes

Let us explore further on what abstraction is and how java implements these concepts.

What is Abstraction?

"Abstraction of data is a property in which only the required details are displayed to the user. The remaining details that are trivial and not required are made sure to not reach the user."
There are two types of Abstraction namely,
Data Abstraction:In this type of abstraction we hide the complex details by creating different data types. We only show the required details to the user.
Control Abstraction: This abstraction gathers all the control statements that are present in the program and shows them as a package.This forms the most important part of any structured programming/

Examples to understand Abstraction

Example 1: The most common example given to explain abstraction is that of a car and its working.
Imagine a person driving a car. The controls he/she has are the accelarator, brakes and clutch. He/She knows when there's an obstacle he/she has to apply the brakes. But he/she isn't aware of how the brakes work. If we think about it, we aren't aware of anything about how the car works internally until we study about it in detail. This is the basic idea of Abstraction.
Example for abstraction using Car Example 2: Another example which can explain abstraction is that of a restaurant. Imagine going to your favourite Restaurant and ordering your favourite food. Most of the times, at restaurants, the making of the food isn't shown to the customers. But, the food is made available to us. This is another illustration of abstraction where the preparation of the food isn't shown to us but the food is provided to us. Example for abstraction using Restaurant

Implementation of Abstraction in Java

Java consists of a non-access modifier called "abstract" that can be used to implement abstraction. Classes and methods can both utilise this abstract modification, but variables cannot.
Complete abstraction is provided by the interface, which means that it only offers method prototypes—not their implementation. A partial abstraction is offered by an abstract class, wherein at least one method is not required to be implemented.

//abstract class
abstract class Food{  
    abstract void Order();  
//concrete class
class Pizza extends Food{  
    void Order(){
class Main{
    public static void main(String args[]){  
        Food f1 = new Pizza();   
        f1.Order();          //call the method  



In the above code snippet, we have a class Food. The class food consists of a Order method. This class is inherited by the Pizza class. In this Pizza class we implement the Order method. The abstract class is first defined, then inherited and then implemented in the program.

Abstract Class

A class that has the keyword "abstract" in its declaration and that cannot be instantiated is said to be an Abstract Class in an Object Oriented Programming construct. These classes contain the abstract methods. Abstract methods are methods which have just the definitions of the method but not implementation.
There may or may not be any abstract methods in an abstract class (a method that has no implementation).

Abstract Class and its characteristics

How to create an Abstract Class

We can create an abstract class using the syntax shown below.

abstract class <NameofClass>(parameters)
            public abstract void Method1();  //Abstract Method
            public void Method2()  //Normal Method
                    //Body of the method

Advantages of Abstraction

1. The fundamental advantage of employing abstraction in programming is that it enables you to assemble a number of related classes as siblings.

2. Abstraction is one of the best feature of Object Oriented programming which helps us to reduce the complexity of a product and makes the software implementation quite easy.

3. The code is dispersed over numerous files and is no longer with in analyst's brain. The fact that people can understand what is happening in their personal language gives businesses more confidence.

Learn More

Usage of the abstract class

Abstract classes are mostly used when many subclasses perform the same operation in various methods through different ways of implementation. Additionally it extends the same abstract class and provides many abstract method implementations.
The class hierarchy used in Object Oriented Programming is explained using Abstract Classes. In order to provide the abstract class's implementation details, it also describes subclasses. This implementation is shown below in the code snippet.

abstract class Vehicle{
    abstract void start () 
 abstract void accelerate ();;
    abstract void stop ();
    abstract void brake ();
class Car extends Vehicle{
    void start () {}
    void stop () { }
class Bike extends Vehicle{
    void start () {}
    void brake () {}
class Scooter extends Vehicle{
    void accelerate () {}
    void brake () {}

In order to give the user with simply the external perspective of a system that has common techniques or operations to describe, we generally choose abstraction. Therefore, we remove the common methods, abstractly express them, and group these abstract methods into a common abstract class. Following the representation of a system's general structure as an abstract class and all the general operations as abstract methods, we are then able to derive any number of classes from the provided abstract class and override the abstract methods to perform these operations for each class. Designing a system becomes valuable in this approach.


The abstract class is shown above. Interfaces are yet another set of construction elements that support abstraction. Interfaces represent a contract and classes that implement interfaces must uphold the contracts.
Interface contracts are nothing more than unimplemented methods. Interfaces consists of just the prototypes of the methods. No single implementation of any methods will exist inside an interfaces.

Declaration of an Interface

public interface MyInterface{
    void myMethod ();

Any class implementing the MyInterface class has to override the 'myMethod' method.

Differences between Abstract Class and Interface

Abstract ClassInterface
Can have abstract or normal methodsCan have only abstract methods
Can have final or non-final, static or non-static variables.Can have final and static variables
Inherited using 'extends' keywordImplemented using 'implements' keyword.
May provide the implementation of InterfaceCan not provide the implementation of Abstract Class
can extend many classes or implement other interfacesCan only implement an interface

Difference between Abstraction and interface

Brief about Encapsulation

Encapsulation's purpose is to ensure that "sensitive" data is obscured from users. In order to do this, one must declare the methods and datamembers of the class as private or provide a get method or a set method to modify the value of a variable which is private.
The reasons for using Encapsulation is to get a better control of data members and methods of the class. The class is flexible as it helps the programmer change the required part of code without disturbing the whole code. Security of data is more. The below code snippet implements this feature.

public class Student{  
private String StudentName;
public String getName(){  
return StudentName;  

public void setName(String name){  

Differences between Abstraction and Encapsulation

As abstraction and encapsulation offer similar services, they can often be a confusion. Let us understand the difference between these to important concepts of OOPs.

It is the process of accesing and abstracting the informationIt is the process of binding the information
used in Interface or design level.used in the Implementation level.
Hides detailsBinds the details into one package.
Usage of abstract classUsage of access modifiers
In abstraction, only the required view is shown to the user and the other details are hidden.In encapsulation, data is bound into a unit and it can be secured using access modifiers.


This concludes our discussion of "Abstraction in Java". I hope that you gained knowledge about this amazing feature offered by java and are now more interested to dive into the language. You can consult the Java Tutorial for further guidance.

Good luck and happy learning!