Enums

Enumeration types in OOP

Overview

Enums (Enumerations) are special data types that define a fixed set of named constants. They make code more readable, type-safe, and self-documenting by replacing magic numbers and strings with meaningful names.

Enums are particularly useful in LLD for representing fixed states, types, or categories that won't change frequently.

Key Concepts

Fixed set of named constants

Type-safe - compiler prevents invalid values

Can have fields, constructors, and methods

Implicitly final and static

Each enum constant is an instance of the enum type

Code Example

java
1// Basic Enum
2public enum VehicleType {
3    CAR, MOTORCYCLE, TRUCK, BUS
4}
5
6// Enum with fields and methods
7public enum OrderStatus {
8    PENDING("Order is pending", false),
9    CONFIRMED("Order is confirmed", false),
10    SHIPPED("Order has been shipped", false),
11    DELIVERED("Order delivered successfully", true),
12    CANCELLED("Order was cancelled", true);
13    
14    private final String description;
15    private final boolean isFinalState;
16    
17    // Constructor
18    OrderStatus(String description, boolean isFinalState) {
19        this.description = description;
20        this.isFinalState = isFinalState;
21    }
22    
23    public String getDescription() {
24        return description;
25    }
26    
27    public boolean isFinalState() {
28        return isFinalState;
29    }
30    
31    // Check if transition is valid
32    public boolean canTransitionTo(OrderStatus newStatus) {
33        if (this.isFinalState) return false;
34        
35        switch (this) {
36            case PENDING:
37                return newStatus == CONFIRMED || newStatus == CANCELLED;
38            case CONFIRMED:
39                return newStatus == SHIPPED || newStatus == CANCELLED;
40            case SHIPPED:
41                return newStatus == DELIVERED;
42            default:
43                return false;
44        }
45    }
46}
47
48// Usage
49public class Order {
50    private OrderStatus status = OrderStatus.PENDING;
51    
52    public void updateStatus(OrderStatus newStatus) {
53        if (status.canTransitionTo(newStatus)) {
54            status = newStatus;
55            System.out.println(newStatus.getDescription());
56        } else {
57            throw new IllegalStateException(
58                "Cannot transition from " + status + " to " + newStatus);
59        }
60    }
61}

OrderStatus enum contains business logic for state transitions, making the code more maintainable and self-documenting.

When to Use

  • Fixed set of states (OrderStatus, PaymentStatus)

  • Types of entities (VehicleType, UserRole)

  • Days of week, months, directions

  • Configuration options with limited values

  • Replacing integer or string constants

Advantages

  • Type safety - compiler catches invalid values

  • Readability - self-documenting code

  • IDE support - autocomplete and refactoring

  • Can contain logic and data

  • Works well with switch statements

Best Practices

  • 1.

    Use enums instead of magic numbers or strings

  • 2.

    Add meaningful fields and methods when needed

  • 3.

    Use for state machines and status tracking

  • 4.

    Consider enum for Strategy pattern implementation

  • 5.

    Keep enum values immutable

💡 Interview Tips

  • Show how enums prevent invalid states

  • Demonstrate enums with methods and fields

  • Use in parking lot (VehicleType, SpotType) or order systems

  • Mention singleton implementation using enum

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In
Enums - OOP Fundamentals | LLD | Revise Algo