Skip to content

Abstract classes

  • created using abstract modifier
  • If a class defines one or more abstract methods, then the class itself must be declared abstract
  • An abstract class can declare both abstract and concrete methods
  • An abstract class can be subclassed, but it can’t be instantiated
  • A subclass derived from an abstract class must either implement all the base class’s abstract methods or be abstract itself

When to use & prefer abstract classes over interfaces?

Section titled “When to use & prefer abstract classes over interfaces?”
  • code reuse is a very compelling reason to use abstract classes, as long as the “is-a” relationship within the class hierarchy is preserved
  • We want to encapsulate some common functionality in one place (code reuse) that multiple, related subclasses will share
  • We need to partially define an API that our subclasses can easily extend and refine
  • The subclasses need to inherit one or more common methods or fields with protected access modifiers