Learning Java from 0 to Senior 5/100: Interfaces and Abstract Classes
Interfaces and Abstract Classes — Structuring Complex Java Applications
Introduction to Abstraction in Java
Abstraction, a key concept in object-oriented programming, allows complex real-world ideas to be represented in simpler, more manageable forms. In Java, abstraction is achieved using abstract classes and interfaces, enabling objects to be defined in terms of their high-level functionality rather than their detailed workings.
Abstract Classes: The Partial Blueprint
Abstract classes are declared using the abstract
keyword and are incomplete by themselves. They require subclasses to provide implementations for the abstract methods.
abstract class Animal {
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep() {
System.out.println("Zzz");
}
}
Classes that inherit from an abstract class:
- Must implement all abstract methods, or
- Become abstract classes themselves.