Nearby lessons
49 of 125Java - Enums
📌 What You Will Learn
- What class and object mean, with real-life examples
- The four pillars: Encapsulation, Abstraction, Inheritance, Polymorphism
- Constructors, this, super, static and final
- Method overloading and method overriding (with a comparison table)
- Abstract classes and interfaces (including the modern changes)
- Association, Aggregation and Composition
Enums is a core concept of the Java language. This lesson explains Enums — Fixed Set of Constants with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Enums — Fixed Set of Constants
An enum is a special kind of class that stores a fixed set of constant values — like the days of a week or the colours of a traffic light. It was added in Java 5 and is perfect for values that never change.
Important facts about enums:
- Every enum is a final class that automatically extends java.lang.Enum.
- Its constants are public static final — accessed like Day.MONDAY.
- An enum is a full class: it can have fields, constructors, methods and even implement interfaces.
- switch works perfectly with enums, making menu-driven code clean and safe.
Example01
Enums — Fixed Set of Constants
Trainer's Note: Use an enum whenever you have a small, known list of values (status, type, category). It is safer than String constants because the compiler checks the values — typos are caught at compile time, not at run time.
Example02
📝 Key Takeaways
- Class = blueprint; Object = real thing made from it (memory in heap).
- Four pillars: Encapsulation (hide data), Abstraction (show only essentials), Inheritance (reuse parent code), Polymorphism (one name, many forms).
- Overloading = same class, different parameters, decided at compile time. Overriding = child redefines parent method, decided at run time.
- Constructor initialises the object; name = class name, no return type.
- this = current object; super = parent class; static = belongs to class; final = cannot change.
- abstract class cannot be instantiated; interface is a contract a class implements.
- Association (weak), Aggregation (has-a, part independent), Composition (strong has-a, part dies with whole).
🧠 Test Your Knowledge
1 QuestionsProgress: 0 / 1