7.4 Abstraction & Interface
In Object-Oriented Design, Abstraction is the process of modeling a complex real-world concept programmatically by extracting only the essential, common features and behaviors while hiding unnecessary implementation details.
Java provides two main tools to implement abstraction:
- Abstract Classes
- Interfaces
1. Abstract Class
An abstract class is like an incomplete blueprint. It's a class that contains methods where exactly how they work hasn't been fully defined yet (unimplemented methods).
If a class contains even a single unimplemented method (an abstract method), the class itself must be declared as abstract.
// Using the abstract keyword to declare an abstract class
abstract class Player {
int currentPos; // Can hold regular variables
Player() { // Can have constructors
currentPos = 0;
}
// Abstract methods: they have a declaration but no body {}
abstract void play(int pos);
abstract void stop();
// Can also hold fully implemented normal methods
void play() {
play(currentPos);
}
}
Key Characteristics of Abstract Classes
- A class tagged with
abstractcannot be instantiated directly.(e.g.,new Player();is illegal). - It can only be completed by a child class inheriting from it. The child class is forced to override and provide implementations for all inherited abstract methods.
- It is used when you want to provide a common skeletal structure among related classes while delegating some specific functionalities for the children to implement.
2. Interface
An Interface is like an empty shell. It provides a much stronger form of abstraction than an abstract class. It generally serves as a "basic sketch" without any implementation, or as a "standard contract" for how objects communicate with each other.
// Declared with the interface keyword
interface Fightable {
// All constants in an interface are implicitly public static final
int MAX_HP = 100;
// All methods in an interface are implicitly public abstract (except default/static in Java 8+)
void attack();
void move(int x, int y);
}
// Interfaces are implemented using the implements keyword
class Fighter implements Fightable {
// You MUST override all abstract methods with public visibility
@Override
public void attack() {
System.out.println("Attacks with fists.");
}
@Override
public void move(int x, int y) {
System.out.println("Moving to coordinates " + x + ", " + y);
}
}
Powerful Features of Interfaces
- Support for Multiple Implementation: A class can only inherit (
extends) from one parent class, but it can implement (implements A, B, C) multiple interfaces simultaneously.class SmartPhone extends Phone implements Camera, MP3Player, GPS { }
- Maximizing Polymorphism: Completely unrelated classes that lack a common parent class inheritance can still be grouped and managed together under a single interface type, as long as they implement that same interface.
- Standardization: In large team projects, providing interfaces first sets the grand architectural frame. As long as developers adhere to those interfaces, independent modules easily plug together later.
3. Abstract Class vs. Interface
| Feature | Abstract Class (abstract class) | Interface (interface) |
|---|---|---|
| Inheritance/Implementation | extends (Single Inheritance) | implements (Multiple Implementation) |
| Variables | Can declare normal member variables | Can only declare constants (static final) |
| Methods | Can have both abstract methods and implemented methods | Traditionally, exclusively abstract methods |
| Primary Purpose | To create a ** common parent class**classifying related objects (IS-A relationship) | To assign ** identical functionalities (a standard contract)**to unrelated objects (CAN-DO relationship) |
Proper use of interfaces and abstract classes at the right times is the ultimate shortcut to architecting robust, flexible software in Java.