0Pricing
Java Academy · Lesson

Abstract Classes

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

Abstract Classes is a free Java Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.");
  }
}

Why use abstract classes?

They capture shared code in one place and force important methods to be provided by each subclass. Use them when some behavior is common, but some varies across concrete types.

Code: implement abstract method

Subclasses must implement abstract methods. Concrete methods from the abstract class are inherited and reusable.

public class Main {
  static abstract class Animal {
    abstract void speak();
    void info() {
      System.out.println("I am an animal.");
    }
  }

  static class Dog extends Animal {
    // Must implement speak()
    void speak() {
      System.out.println("Woof!");
    }
  }

  public static void main(String[] args) {
    Dog d = new Dog();      // allowed: Dog is concrete
    d.info();               // inherited concrete method
    d.speak();              // Dog's implementation
  }
}

Rules & mix

Rules: an abstract class may have fields, constructors, and concrete methods. A subclass becomes concrete only after it implements all abstract methods; otherwise it is abstract too.

Code: abstract + polymorphism

Abstract classes work well with polymorphism. Code can use the abstract type, and each subclass provides the right behavior.

public class Main {
  static abstract class Animal {
    abstract void speak();
  }

  static class Dog extends Animal {
    void speak() { System.out.println("Woof!"); }
  }

  static class Cat extends Animal {
    void speak() { System.out.println("Meow!"); }
  }

  public static void main(String[] args) {
    // Polymorphism through abstract base type
    Animal[] pets = { new Dog(), new Cat() };
    for (int i = 0; i < pets.length; i = i + 1) {
      pets[i].speak(); // each subclass provides its own behavior
    }
  }
}

Abstract class check

Quick check: Which statement about abstract classes is correct?

Recap

Recap: Use abstract classes to share code and require key methods. You cannot instantiate them directly, but subclasses complete the behavior.

Frequently asked questions

Is the “Abstract Classes” lesson free?

Yes — the full text of “Abstract Classes” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Abstract Classes”?

Declare abstract classes and methods, understand why they cannot be instantiated, and see how subclasses complete the design. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Abstract Classes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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