Blog header background

The Power of Java Abstraction

Updated on March 19, 2024

5 min read

Copy link
Share on WhatsApp

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
        super(color);
        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
        super(color);
        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);
 
        System.out.println(s1.toString());
        System.out.println(s2.toString());
    }
}
Output:
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
brochure-banner-bg

POSTGRADUATE PROGRAM IN

Multi Cloud Architecture & DevOps

Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.

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
skill-test-section-bg

82.9%

of professionals don't believe their degree can help them get ahead at work.

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

Conclusion

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.

FAQs
How does abstraction simplify code and promote code reusability?
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.
What are the benefits of using abstraction in Java programming?
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.
When to use Abstract Methods & Abstract Class?
You should use abstract methods and abstract classes for sharing code among several closely related classes.
What are the 2 types of abstraction in Java?
The two types of abstraction in Java are data abstraction and process abstraction.
What are some best practices for effectively applying abstraction in Java projects?
You can use an abstract class or an interface to apply abstraction in Java projects.

Updated on March 19, 2024

Link

ARTICLES

Upskill with expert articles

11 In-Demand Backend Developer Skills You Should Master

11 In-Demand Backend Developer Skills You Should Master

Discover the essential backend developer skills: mastering programming languages, database management, API integration, security practices, DevOps, and more!

8 mins read
Top Front End Developer Skills You Should Master to Succeed

Top Front End Developer Skills You Should Master to Succeed

Explore top front end developer skills, including key technical and soft skills to become a front end developer. Know the salary insights and future opportunities.

8 mins read
Top Full Stack Project Ideas to Boost Your Development Skills in 2025

Top Full Stack Project Ideas to Boost Your Development Skills in 2025

Explore the best full stack project ideas for beginners and advanced levels. Build real-world projects to showcase your expertise. Learn project ideas for full stack development in 2025.

8 mins read
Difference Between Procedural and Object Oriented Programming

Difference Between Procedural and Object Oriented Programming

Understand the differences between Procedural and Object-Oriented Programming. Learn which to choose for your projects and why.

8 mins read
What’s the Difference Between Internet and WWW (World Wide Web)?

What’s the Difference Between Internet and WWW (World Wide Web)?

Discover the difference between Internet and WWW, their roles, and how they work together in the digital world.

8 mins read
Difference Between Algorithm and Flowchart – Explained!

Difference Between Algorithm and Flowchart – Explained!

Explore the difference between algorithm and flowchart, their advantages, practical uses, and how they simplify problem-solving in programming and beyond.

8 mins read
Top 12 Trending Technologies to Learn in IT Industry

Top 12 Trending Technologies to Learn in IT Industry

Stay future-ready with insights on the best technology to learn in 2025. Discover the latest trends and skills that will propel your career in the dynamic tech landscape.

8 mins read
What is Abstraction in Java and How to Implement It?