Hero Vired Logo


Vired Library

Complimentary 4-week Gen AI Course with Select Programs.

Request a callback

or Chat with us on

The Power of Java Abstraction

In Java, abstraction ensures that users only have access to important details. Non-essential and trivial units are not displayed to the user. Dive into this article to learn more about data abstraction in Java. 

What is Abstraction in Java?

Abstraction in Java can be described as the process of spotting the required features of an object while ignoring the irrelevant details. The properties and behaviors of an object separate it from other objects of the same type. They also help with the classification or grouping of objects. 

Example of Abstraction in Java

Take a look at one abstraction in Java example:

// Java program to illustrate the
// concept of Abstraction
abstract class Shape {
    String color;
    // these are abstract methods
    abstract double area();
    public abstract String toString();
    // abstract class can have the constructor
    public Shape(String color)
        System.out.println("Shape constructor called");
        this.color = color;
    // this is a concrete method
    public String getColor() { return color; }
class Circle extends Shape {
    double radius;
    public Circle(String color, double radius)
        // calling Shape constructor
        System.out.println("Circle constructor called");
        this.radius = radius;
    @Override double area()
        return Math.PI * Math.pow(radius, 2);
    @Override public String toString()
        return "Circle color is " + super.getColor()
            + "and area is : " + area();
class Rectangle extends Shape {
    double length;
    double width;
    public Rectangle(String color, double length,
                     double width)
        // calling Shape constructor
        System.out.println("Rectangle constructor called");
        this.length = length;
        this.width = width;
    @Override double area() { return length * width; }
    @Override public String toString()
        return "Rectangle color is " + super.getColor()
            + "and area is : " + area();
public class Test {
    public static void main(String[] args)
        Shape s1 = new Circle("Red", 2.2);
        Shape s2 = new Rectangle("Yellow", 2, 4);
Shape constructor called
Circle constructor called
Shape constructor called
Rectangle constructor called
Circle color is Redand area is : 15.205308443374602
Rectangle color is Yellowand area is : 8.0

Enroll: Business Analytics and Data Science course

What is an Abstract Class?

An abstract class in Java contains the abstract keyword in its declaration. Abstract classes may or may not include an abstract method in Java. If a class comes with at least one abstract method, you will have to declare the class abstract. 


A class cannot be instantiated if a class is declared abstract. If you want to use an abstract class, you should be able to inherit it from another class. You will be required to inherit an abstract class from another class to use it. Once you inherit an abstract class in Java, you will be required to provide implementations to all abstract methods within it. 


Learn: Thread in Java


Advantages of Using Abstraction in Java Programming

The different advantages of using abstraction in Java programming are as follows:

  • Abstraction can reduce the complexity of viewing things in Java. 
  • Java abstraction can increase code reusability by preventing code duplication.
  • Java abstraction can help increase the security of a program or application by making only essential details accessible to users.
  • Java abstraction can help with improving the maintainability of an application.
  • Abstraction is useful for enhancing the modularity of the application. 
  • Improvements become easier because you will be able to perform various modifications in the internal system without affecting end-users. 
  • Java abstraction can hide implementation details and display only relevant information.
  • It ensures that your users get a simple and clear interface.
  • Abstraction maximizes security by restricting access to internal class details. 
  • Abstraction supports modularity because complex systems can be segmented into smaller and more manageable parts. 
  • Java abstraction can hide implementation complexity details from the user, making the code easier to use and understand. 
  • Abstraction supports a lot of flexibility in terms of implementing a program. Adjustments to the underlying implementation details can be completed without impacting the user-facing interface. 
  • Java abstraction allows separation and modularity of concerns. It makes code more maintainable and easier to debug.

What is an Abstract Method and Its Implementation?

If you want a class to include a specific method, but you want the original implementation of that method to be specified by child classes, you will have to declare the method in the parent class as an abstract. 

One example of the abstract method in Java is as follows:

abstract void printStatus();//no method body and abstract

Read: What is Static Method in Java with Examples

Introduction to Interfaces and Their Purpose in Abstraction

Interfaces are one of the most powerful tools to attain abstraction in Java. Since interfaces are like abstract classes, they cannot be instantiated. As they don’t provide an implementation, you won’t have a constructor either. 

Defining Interfaces and their Syntax in Java

The syntax for the abstract class and the interface is as follows:

public interface Hittable {
  int calculateDamage(int strength, double velocity, double angle);

Example of Abstract Class That Has an Abstract Method

One abstract class in Java example with an abstract method is as follows: //Example of an abstract class that has abstract and non-abstract methods abstract class Bike{ Bike(){System.out.println(“bike is created”);} abstract void run(); void changeGear(){System.out.println(“gear changed”);} } //Creating a Child class which inherits Abstract class class Honda extends Bike{ void run(){System.out.println(“running safely..”);} } //Creating a Test class which calls abstract and non-abstract methods class TestAbstraction2{ public static void main(String args[]){ Bike obj = new Honda(); obj.run(); obj.changeGear(); } }


Explore: Access modifiers in Java


You will be able to create abstract classes and methods with the help of the abstract keyword. An abstract method does not come with any implementation. Any class containing abstract methods also needs to be abstract. But you won’t be able to create objects using an abstract class. 


If you want to implement features of an abstract class, you will have to inherit the subclass from it and create objects of the subclass. A subclass should be able to override all abstract methods in an abstract class.


Abstraction in Java can simplify code by breaking it down into smaller and easily manageable components. Abstraction enables developers to create modular and reusable code that can be used in various contexts.
The use of abstraction in Java programming is extremely useful for writing shorter codes. Moreover, abstraction classes support code reusability and also prevent code duplication. The abstract class in Java are also useful for changing the internal code without impacting classes.
You should use abstract methods and abstract classes for sharing code among several closely related classes.
The two types of abstraction in Java are data abstraction and process abstraction.
You can use an abstract class or an interface to apply abstraction in Java projects.

High-growth programs

Choose the relevant program for yourself and kickstart your career

You may also like

Carefully gathered content to add value to and expand your knowledge horizons

Hero Vired logo
Hero Vired is a premium LearnTech company offering industry-relevant programs in partnership with world-class institutions to create the change-makers of tomorrow. Part of the rich legacy of the Hero Group, we aim to transform the skilling landscape in India by creating programs delivered by leading industry practitioners that help professionals and students enhance their skills and employability.

Data Science

Accelerator Program in Business Analytics & Data Science

Integrated Program in Data Science, AI and ML

Accelerator Program in AI and Machine Learning

Advanced Certification Program in Data Science & Analytics


Certificate Program in Full Stack Development with Specialization for Web and Mobile

Certificate Program in DevOps and Cloud Engineering

Certificate Program in Application Development

Certificate Program in Cybersecurity Essentials & Risk Assessment


Integrated Program in Finance and Financial Technologies

Certificate Program in Financial Analysis, Valuation and Risk Management


Certificate Program in Strategic Management and Business Essentials

Executive Program in Product Management

Certificate Program in Product Management

Certificate Program in Technology-enabled Sales

Future Tech

Certificate Program in Gaming & Esports

Certificate Program in Extended Reality (VR+AR)

Professional Diploma in UX Design

In the News
About Us
Contact us
Vired Library
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

© 2024 Hero Vired. All rights reserved