3

Since the Java 9 release is there any good reason to use abstract classes anymore? It seems like you can do everything with interfaces now, plus you can have multiple inheritance with interfaces.

Java SE 9: Interfaces

  • Constant variables
  • Abstract methods
  • Default methods
  • Static methods
  • Private methods
  • Private Static methods

Am I missing something? I can't find any good reason except, probably, backward compatibility.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
Sergei Sirik
  • 1,249
  • 1
  • 13
  • 28
  • 6
    Plenty of abstract classes contain instance-level fields, and could therefore not be converted to an equivalent interface. Perhaps you want to force inheritance from a particular super class to guarantee you have package-level access to some of its internals. The second case is perhaps less common but you can see it employed in parts of the JDK (e.g., in socket selector APIs). – Mike Strobel Oct 02 '17 at 19:21

1 Answers1

3

Indeed, you're missing something:

  • non-constant variables
  • protected and package-private methods

In general, you don't have to use a feature just because you can. In particular, be careful with multiple inheritance. It can make things complicated very quickly.

Tamas Rev
  • 7,008
  • 5
  • 32
  • 49