Home

 › 

Articles

 › 

Understanding Abstraction in Java, with Examples

Types of Methods in Java

Understanding Abstraction in Java, with Examples

Key Points

  • Abstraction in Java involves hiding implementation details to focus on essential functions.
  • Encapsulation, abstract classes, and interfaces are key concepts for promoting abstraction in Java.
  • Abstract classes act as blueprints for other classes and define common behavior between subclasses.
  • Interfaces are minimal and require any implementing class to include specific methods.
  • Abstraction is crucial for maintaining good structure and adaptability in large Java programs.

Understanding abstraction in Java is easy if you can grasp a few simple concepts. In programming, managing the complexity of a program is extremely important. Commonly, you would do this largely through abstraction.

Abstraction is a fundamental object-oriented programming concept, so let’s dive and learn a bit about it.

What Is Abstraction in Java?

At a basic level, abstraction is the hiding of implementation details of a piece of a program so that you can focus on its essential functions.

Most of the time, abstraction is just the process of modeling real-world entities as simplified versions that fit within your program. However, to achieve this simplification, non-essential details are ignored, and the most important details must be the focus. 

One foundational concept of abstraction is the idea of encapsulation, where we define clear boundaries of what each part of a program needs to know. This makes a program much more adaptable, as you do not need to worry about the low-level details of a system when modifying another system that relies on it.

Encapsulation is often done through classes. Based on the access modifiers of methods and fields, other classes only know what they need to know about each other, streamlining the interactions between them.

However, there are two other ways that you can promote abstraction in Java: abstract classes and interfaces.

Implementing Abstraction in Java

abstraction in java IDE example
AbstractAnimal is an abstract class that defines a method sound() without providing its implementation.

©History-Computer.com

Abstract Classes

If you read our article on Class in Java, you’ll know that Abstract classes are a great example of how you can model real-world entities in Java. They act as blueprints for other classes.

And as such, they cannot be instantiated themselves. Abstract classes define common behavior between their subclasses. So, the subclasses are allowed to implement the properties as they see fit.

Let’s check out a quick example. Here’s how we could make an abstract class for shapes:

abstract class Shape {
    protected String color;

    public Shape(String color) {
        this.color = color;
    }

    public abstract double calculateArea();

    public void display() {
        System.out.println("This is a " + color + " shape.");
    }
}

As you can see here, the Shape class defines some generic details about shapes, but it does go any further than that. 

Any shape can have a color, so that field is included. Also, a given shape has a way that its area can be calculated, but that will vary wildly depending on what it looks like.

As such, we can declare a method called calculateArea without actually saying what it does, leaving it up to the subclasses. By putting this here, all subclasses are required to implement this method.

class Rectangle extends Shape {
    private double length;
    private double width;

    public Rectangle(String color, double length, double width) {
        super(color);
        this.length = length;
        this.width = width;
    }

    @Override
    public double calculateArea() {
        return length * width;
    }
}

As usual, we can use extends to make Rectangle a subclass of Shape. Two new fields are defined: length and width. We then override the calculateArea method using these fields, providing the first actual implementation of this functionality. The Rectangle class can now be instantiated and used like any other class.

Interfaces

Interfaces are similar to abstract classes, but there are some key differences. In the abstract class example, we included a display method that had a body, but we can’t do the same with interfaces. Methods in interfaces never include an implementation.

Also, a class can only inherit from one other class, but it can inherit from as many interfaces as needed. Finally, while abstract classes can have constructors, interfaces cannot.

Here’s an example of an interface that models an animal:

interface Animal {
    void eat();
    void sleep();
}

As can be seen, interfaces are very minimal. By including an eat and sleep method, we require any class that implements this interface to implement those methods.

Here’s how we can make a Dog class using this interface:

class Dog implements Animal {
    @Override
    public void eat() {
        System.out.println("Dog is eating.");
    }

    @Override
    public void sleep() {
        System.out.println("Dog is sleeping.");
    }
}

Instead of using extends, the keyword implements is used to tell Java that an interface is being inherited. As required by the interface, we implement the eat and sleep methods, making them appropriate for a dog.

Abstraction in Java: Wrapping Up

When writing large programs in Java, abstraction becomes increasingly important for upholding good structure and maintainability. Since Java is an object-oriented language, these concepts carry over in many different situations.

There are so many more ways of using abstraction in Java, so keep learning! Now, let’s check out some frequently asked questions.

Summary Table

ConceptDescription
AbstractionHiding implementation details to focus on essential functions
EncapsulationDefining clear boundaries between parts of a program
Abstract ClassesBlueprints for other classes, cannot be instantiated
InterfacesSimilar to abstract classes, but methods never include implementation

Frequently Asked Questions

How does an abstract class work in Java?

An abstract class in Java is a class that can’t be instantiated, meaning you can’t create objects from it directly. Instead, you can only inherit it from another class.

What is abstraction in Java?

Abstraction in Java is a fundamental object-oriented programming concept that emphasizes the idea of “hiding complexity.” It allows you to hide certain details and only show the necessary features of an object.

Why use an abstract class in Java?

Typically, you would use an abstract class when you want to define something with common properties that will be shared among different objects. Maybe, you don’t want to show details of an object for security.

To top