library
beginnerAbstraction in Java
Use abstract classes and interfaces to define contracts without exposing implementation.
Abstraction exposes only essential features while hiding complexity behind a well-defined interface. In Java, it's achieved through abstract classes and interfaces.
A TV remote hides the circuitry. You press volume up without knowing whether it uses infrared or Bluetooth.
Key Concepts
1
An abstract class can have abstract methods (no body) and concrete methods (with body). It can hold state, constructors, and any access modifier. You cannot instantiate it directly.
2
An interface since Java 8 can have default methods and static methods; since Java 9, private methods. But interfaces cannot have instance fields or constructors, and all fields are implicitly public static final.
3
When to use which: abstract class when you have shared state or constructor logic. Interface when defining a capability unrelated classes can implement (Comparable, Serializable). A class can implement multiple interfaces but extend only one abstract class.
4
Abstraction reduces coupling: calling code depends on the abstract type, not the concrete implementation. This is the foundation of dependency inversion, strategy pattern, and testability (mocking interfaces in tests).