Java Abstraction with Abstract Classes – Complete Guide with Examples


Learn abstraction in Java using abstract classes, how to define abstract methods, implement them in child classes, and achieve partial abstraction for flexible and maintainable OOP design.

Abstraction Using Abstract Classes in Java – Complete Detailed Tutorial

Abstraction is an OOP concept that hides implementation details and shows only essential features.

It helps reduce complexity and increase code maintainability.

  1. Abstract Class: A class that cannot be instantiated and may contain abstract methods (without body) and concrete methods (with body).

1. Key Points About Abstract Classes

  1. Defined using the abstract keyword
  2. Can have abstract methods and concrete methods
  3. Cannot be instantiated directly
  4. A subclass must implement all abstract methods or be declared abstract
  5. Supports partial abstraction

2. Syntax of Abstract Class


abstract class Shape {
// abstract method (no body)
abstract void draw();

// concrete method
void showInfo() {
System.out.println("This is a shape");
}
}

class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing Circle");
}
}

3. Example – Basic Abstract Class


abstract class Animal {
// abstract method
abstract void sound();

// concrete method
void eat() {
System.out.println("Animal eats food");
}
}

class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}

public class Main {
public static void main(String[] args) {
// Animal a = new Animal(); // Not allowed, abstract class cannot be instantiated
Dog d = new Dog();
d.sound(); // overridden method
d.eat(); // inherited concrete method
}
}

Output:


Dog barks
Animal eats food

4. Abstract Class vs Interface

FeatureAbstract ClassInterface
MethodsCan have abstract + concrete methodsOnly abstract (before Java 8), now can have default/static
VariablesCan have instance variablesOnly static & final variables
InheritanceSingle class inheritance (extends)Multiple inheritance (implements)
ConstructorCan have constructorsCannot have constructors
Access ModifiersAnypublic by default

5. Example – Abstract Class with Multiple Subclasses


abstract class Shape {
abstract void draw();
}

class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing Circle");
}
}

class Rectangle extends Shape {
@Override
void draw() {
System.out.println("Drawing Rectangle");
}
}

public class Main {
public static void main(String[] args) {
Shape s1 = new Circle();
Shape s2 = new Rectangle();

s1.draw(); // calls Circle's draw
s2.draw(); // calls Rectangle's draw
}
}

Output:


Drawing Circle
Drawing Rectangle

Explanation:

  1. Abstract class allows partial abstraction
  2. Provides a common template for multiple subclasses

6. Advantages of Using Abstract Classes

  1. Supports partial abstraction
  2. Can have code reuse (concrete methods)
  3. Provides template for subclasses
  4. Helps in polymorphism (runtime method calls)
  5. Useful when multiple related classes share some methods

7. Summary

  1. Abstraction → hide implementation, show essential features
  2. Abstract class → may contain abstract and concrete methods, cannot be instantiated
  3. Subclasses must implement abstract methods
  4. Helps code reuse, flexibility, maintainability, and polymorphism