Backend
Java
Java questions
What are abstract classes and interfaces in Java? When should you use one over the other?

What are abstract classes and interfaces in Java? When should you use one over the other?

An abstract class in Java is a class that cannot be instantiated and can contain both abstract methods and methods with implementation. An interface is a contract that classes can implement and can only contain abstract methods (until Java 8) and methods with default implementation (since Java 8).

Use an abstract class when you want to share code between multiple classes, and an interface when you want to specify a contract that can be implemented by any class in the hierarchy.