understanding abstraction

Understanding Abstraction | Java

Abstraction is a crucial concept in the world of software development and plays a pivotal role in the Java programming language. In this article, we’ll explore what abstraction is, how it is achieved in Java, and when and why to use it in your programming endeavors.

What is Abstraction?

Abstraction is one of the four fundamental principles of Object-Oriented Programming (OOP), alongside encapsulation, inheritance, and polymorphism. At its core, abstraction is the process of simplifying complex reality by modeling classes based on essential attributes and behaviors while hiding the irrelevant details. In other words, it involves defining the structure of a class without specifying the exact implementation.

In Java, abstraction is implemented through abstract classes and interfaces, which serve as blueprints for other classes. These abstract entities define method signatures (i.e., the method’s name, parameters, and return type) but leave the implementation to their concrete subclasses.

How to Achieving Abstraction?


An interface in Java is a contract that defines a set of abstract methods that a class must implement if it claims to implement that interface. Interfaces provide a way to achieve multiple inheritance in Java, as a class can implement multiple interfaces.

public interface Drawable 
    void draw();

A class that implements the Drawable interface must provide an implementation for the draw() method.

Abstract Class

An abstract class is a class that cannot be instantiated and may contain both abstract and concrete methods. Abstract methods are declared using the abstract keyword and do not have a method body. Subclasses of an abstract class must provide concrete implementations for all abstract methods.

public abstract class Shape 
    abstract double area();
    abstract double perimeter();

Subclasses of the Shape class, like Circle or Rectangle, would be required to provide specific implementations for the area() and perimeter() methods.

When and Why to use Abstraction

Abstraction is a powerful concept with several benefits in software development.

  • Simplification and Modularity
    • Abstraction simplifies complex systems by breaking them into smaller, more manageable parts. It allows developers to focus on high-level design and modular development, leading to code that is easier to understand and maintain.
  • Reusability
    • Abstract classes and interfaces promote code reusability. By defining common behaviors in abstract entities, you can ensure that multiple classes adhere to the same contract or share common functionality.
  • Polymorphism
    • Abstraction facilitates polymorphism, allowing objects of different concrete classes to be treated as objects of a common abstract class or interface. This is a key feature of OOP and enables flexible, extensible code.
  • Flexibility and Extensibility
    • Abstraction allows you to extend the functionality of a class without changing its existing code. You can add new concrete subclasses or implement additional interfaces to expand your software’s capabilities.
  • Enforcing Standerds
    • Abstract classes and interfaces enable you to define and enforce coding standards and contracts within a project or organization. This leads to consistent and predictable code.

When to use Abstraction;

  • When designing a framework
    • If you are developing a library or framework for others to use, abstraction is essential to provide a clear and extensible interface for your users.
  • To define common behavior
    • When you have a group of classes that share common behaviors or methods, creating an abstract class or interface ensures that all classes adhere to a standardized contract.
  • To create hierarchies
    • When you want to represent hierarchical relationships among your classes, such as a base class with multiple subclasses, abstraction helps organize and model these relationships effectively.

We will discuss about Polymorphism in next article.

Leave a Reply

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