Learn Java
import com.learnjava.oopfundamentals.Abstraction;

Abstraction & Abstract Classes in Java

@Concept

Abstraction means exposing only what a piece of code needs to know to use something, hiding the messier details of how it actually works underneath — calling ArrayList's add() method requires knowing nothing about resizing arrays internally. An abstract class is one specific Java tool for this: declared with the abstract keyword, it can define abstract methods that have a signature but no body at all, forcing every concrete, non-abstract subclass to supply its own implementation.

An abstract class can never be instantiated directly with new, even though it can still hold regular fields, constructors, and fully-implemented ordinary methods alongside its abstract ones. It exists specifically to be extended, providing shared structure and implementation while leaving certain pieces deliberately unfinished for each subclass to complete in its own way.

@HowItWorks
1

Abstraction hides implementation detail

Using something only requires knowing its interface, not the mechanics behind it.

2

Declaring an abstract class

The abstract keyword on a class means it can never be instantiated directly with new.

3

Abstract methods have no body

Just a signature ending in a semicolon, with the actual implementation left to subclasses.

4

Concrete subclasses must implement every abstract method

Or themselves also be declared abstract, deferring that obligation further down the hierarchy.

5

Abstract classes can still hold real code

Regular fields, constructors, and fully-implemented ordinary methods can sit right alongside the abstract ones.

@Example
abstract class Shape {
abstract double area();
 
void describe() {
System.out.println("Area: " + area());
}
}
 
class Square extends Shape {
double side;
 
Square(double side) {
this.side = side;
}
 
double area() {
return side * side;
}
}
 
public class AbstractDemo {
public static void main(String[] args) {
Shape s = new Square(4);
s.describe();
}
}
Output
Area: 16.0
1Shape declares area() as abstract, with no body, leaving the actual calculation to each subclass.
2Square, a concrete subclass, is required to implement area(), and does, as side * side.
3describe(), a fully-implemented ordinary method already living in Shape, calls area() without knowing which subclass it's actually working with.
4Because s refers to an actual Square object, that call runs Square's implementation, printing 16.0 as the area.
@UseCases
Defining a shared method signature that every subclass must implement in its own way, like area() for different shapes
Providing common, already-implemented behavior in a superclass alongside methods each subclass must finish itself
Preventing a class that only makes sense as a base for other classes from ever being instantiated directly
Designing a class hierarchy around what varies, like each shape's area formula, versus what stays the same

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.