0Pricing
Java Academy · Lesson

Abstract Classes

Declare abstract classes and methods, understand why they cannot be instantiated, and see how subclasses complete the design.

What is an abstract class?

Abstract class: defines a common idea and may leave some methods without a body. It can also include normal methods with code. Think: a template that requires subclasses to fill the blanks.

Code: cannot instantiate

Abstract classes cannot be instantiated directly. They can still contain regular methods that all subclasses inherit.

public class Main {
  // Abstract class with one abstract method and one concrete method
  static abstract class Animal {
    abstract void speak();              // must be implemented by subclasses
    void info() {                       // concrete method allowed
      System.out.println("I am an animal.");
    }
  }

  public static void main(String[] args) {
    // Animal a = new Animal(); // Uncommenting would not compile: cannot instantiate abstract class
    System.out.println("You cannot create an Animal directly if it is abstract.");
  }
}

All lessons in this course

  1. Abstract Classes
  2. Interfaces (intro & contrasts)
  3. Up/Down Casting & instanceof
← Back to Java Academy