Course contentsShow
← Java
Lesson 481 of 2,87012. Abstract Classes and InterfacesFree lesson

Concrete Methods in Abstract Classes

Mixing abstract and concrete methods in the same abstract class to share common implementation across subclasses.

Concrete Methods in Abstract Classes

What You'll Learn

You'll learn how to mix abstract and concrete methods in the same abstract class to share common implementation across subclasses.


The Core Idea

Abstract classes aren't limited to just abstract methods—they can also contain concrete methods (methods with full implementations). This powerful combination lets you define common behavior once in the abstract class while still forcing subclasses to implement specific abstract methods.

Think of it like a recipe template: the abstract class provides some ready-to-use steps (concrete methods) that every dish will share, while leaving certain steps (abstract methods) for each specific recipe to define.

Why Mix Concrete and Abstract Methods?

When multiple subclasses share identical behavior, you don't want to copy-paste that code into each one. Instead, implement it once as a concrete method in the abstract class. All subclasses inherit it automatically.

abstract class Animal {
    // Concrete method - shared implementation
    public void sleep() {
        System.out.println("Sleeping...");
    }
    
    // Abstract method - each subclass must define
    public abstract void makeSound();
}

class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Woof!");
    }
    // sleep() is inherited, no need to rewrite it
}

class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Meow!");
    }
    // sleep() is inherited here too
}

Both Dog and Cat get the sleep() behavior for free, but each provides its own unique makeSound() implementation.

The Benefits

  • Code reuse: Write shared logic once
  • Consistency: All subclasses use the same implementation
  • Flexibility: Force customization only where needed

Key Takeaway: Abstract classes can contain both abstract methods (forcing subclasses to implement) and concrete methods (providing shared functionality), giving you the best of both worlds for building class hierarchies.