Java
Beginner
1 min read
final Classes, final Methods, and Preventing Inheritance
Example
// Final class — cannot be subclassed
final class ImmutableVector {
private final double x, y, z;
ImmutableVector(double x, double y, double z) {
this.x = x; this.y = y; this.z = z;
}
public double getX() { return x; }
public double getY() { return y; }
public double getZ() { return z; }
public ImmutableVector add(ImmutableVector other) {
return new ImmutableVector(x + other.x, y + other.y, z + other.z);
}
public double magnitude() {
return Math.sqrt(x * x + y * y + z * z);
}
@Override public String toString() {
return String.format("Vector(%.2f, %.2f, %.2f)", x, y, z);
}
}
// Composition over inheritance example
class Logger {
void log(String message) {
System.out.println("[LOG] " + message);
}
}
class UserService { // HAS-A Logger, not IS-A Logger
private final Logger logger = new Logger();
public String findUser(int id) {
logger.log("Looking up user " + id);
return "User-" + id; // simplified
}
}
public class FinalAndComposition {
// Class attempting to extend ImmutableVector would be a compile error:
// class ExtendedVector extends ImmutableVector { } // ERROR
public static void main(String[] args) {
ImmutableVector v1 = new ImmutableVector(1, 2, 3);
ImmutableVector v2 = new ImmutableVector(4, 5, 6);
ImmutableVector v3 = v1.add(v2);
System.out.println("v1: " + v1);
System.out.println("v2: " + v2);
System.out.println("v1 + v2: " + v3);
System.out.printf("|v1| = %.4f%n", v1.magnitude());
UserService svc = new UserService();
System.out.println(svc.findUser(42));
}
}