Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java Abstraction: Mastering Abstract Classes, Methods, and Practical Examples

What Is Abstraction in Java?

In Java, abstraction exposes only the essential features of an object, hiding the internal implementation details that are irrelevant to the user. It simplifies development by reducing complexity and effort. Abstraction is achieved through abstract classes, abstract methods, and interfaces, and is a core concept in Java’s object‑oriented design.

In this tutorial you will learn:

Understanding Abstract Classes

An abstract class is a class that cannot be instantiated on its own but can define a common base for related subclasses. It may contain both abstract methods (without bodies) and concrete methods (fully implemented). The defining feature of an abstract class is that it declares at least one abstract method, or it is explicitly marked abstract for design reasons.

Consider the classic Shape hierarchy used in many Java tutorials:

Java Abstraction: Mastering Abstract Classes, Methods, and Practical Examples

The Shape class contains shared attributes and the calculateArea() method signature, while Rectangle, Circle, and Triangle provide concrete implementations. Although the Shape class can be instantiated in theory, doing so would be semantically meaningless and could lead to errors. Therefore it is declared abstract:

abstract class Shape {
    // shared code
}

What Are Abstract Methods?

An abstract method declares a method signature without providing an implementation. It must be defined within an abstract class and overridden by concrete subclasses. Because abstract methods lack a body, they cannot be marked final or static.

Example syntax:

abstract public void calculateArea();

In the Shape example, each subclass must implement calculateArea() with its own formula, ensuring that every shape can report its area while keeping the common interface intact.

Practical Code Example

abstract class Shape {
    abstract void calculateArea();
}

class Rectangle extends Shape {
    void calculateArea() {
        System.out.println("Area of Rectangle");
    }

    public static void main(String[] args) {
        Shape obj = new Rectangle();
        obj.calculateArea();
    }
}

Key Takeaways About Abstract Classes

The Final Keyword in Java

The final modifier is used to lock down a class, method, or variable so that it cannot be overridden or altered:

Example: Combining Abstract and Final

abstract class Shape {
    final int b = 20;
    public void display() {
        System.out.println("This is display method");
    }
    abstract public void calculateArea();
}

public class Rectangle extends Shape {
    public static void main(String[] args) {
        Rectangle obj = new Rectangle();
        obj.display();
        // obj.b = 200; // Compile‑time error: cannot assign to final variable
    }
    @Override
    public void calculateArea() {
        System.out.println("Calculating rectangle area");
    }
}

Uncommenting the calculateArea() implementation resolves the compilation error, while attempting to modify b demonstrates the immutability of final variables.

Rules for Abstract Methods

Java

  1. C# Abstract Classes: A Practical Tutorial with Code Examples
  2. Mastering Abstraction in OOP: Java Abstract Classes & Methods Explained
  3. Java String length() Method: How to Get a String’s Size (Example)
  4. Understanding Java String.charAt(): Syntax, Return Type, Exceptions, and a Practical Example
  5. Java String.contains() Method: How to Check for Substrings – Practical Examples
  6. Mastering Java’s String.endsWith(): How to Check String Suffixes with Examples
  7. Polymorphism in Java: A Comprehensive Guide with Practical Examples
  8. Mastering Java's split() Method: A Practical Guide with Code Examples
  9. Master Java Reflection API: A Practical Guide with Code Examples
  10. Java Abstraction Explained: Simplify Code & Boost Readability