0% found this document useful (0 votes)
6 views10 pages

Enums

Enums in Java, introduced in Java 5, are special data types consisting of a set of pre-defined named values. They can be declared inside or outside a class, but not within a method, and can include fields, constructors, and methods. Enums implicitly extend the java.lang.Enum class and can implement multiple interfaces, but cannot inherit from other classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Enums

Enums in Java, introduced in Java 5, are special data types consisting of a set of pre-defined named values. They can be declared inside or outside a class, but not within a method, and can include fields, constructors, and methods. Enums implicitly extend the java.lang.Enum class and can implement multiple interfaces, but cannot inherit from other classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ENUMS

Object Oriented Programming by Mrs Larisse 1/7/2025


What is Enum

• Enum, introduced in Java 5, is a special data type that consists


of a set of pre-defined named values separated by commas.
These named values are also known as elements or
enumerators or enum instances.
• According to Java naming conventions, it is recommended that
we name constant with all capital letters

• One thing to keep in mind is that, unlike classes, enumerations


neither inherit other classes nor can get extended(i.e become
superclass). We can also add variables, methods, and
constructors to it. The main objective of an enum is to define
our own data types(Enumerated Data Types).

Object Oriented Programming by Mrs Larisse 1/7/2025


Declaration of Enum in Java

Enum declaration can be done outside a Class or inside a Class


but not inside a Method.
1.Outside the class

Object Oriented Programming by Mrs Larisse 1/7/2025


Declaration of Enum in Java

2.Inside the class

Object Oriented Programming by Mrs Larisse 1/7/2025


Enums with IF statement

Object Oriented Programming by Mrs Larisse 1/7/2025


Enums with Switch case

Object Oriented Programming by Mrs Larisse 1/7/2025


Fields, Constructors and Methods within Enum

Object Oriented Programming by Mrs Larisse 1/7/2025


Fields, Constructors and Methods within Enum

Price for HP
has been
changed
because it is
not final

Object Oriented Programming by Mrs Larisse 1/7/2025


Looping Enum

Object Oriented Programming by Mrs Larisse 1/7/2025


Enum and Inheritance

• All enums implicitly extend java.lang.Enum class. As a class


can only extend one parent in Java, an enum cannot extend
anything else.
• toString() method is overridden in java.lang.Enum class,
which returns enum constant name.
• enum can implement many interfaces.

Object Oriented Programming by Mrs Larisse 1/7/2025

You might also like