🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In Kotlin, sealed classes and enums (short for enumerations) are ways to represent restricted class hierarchies. A sealed class is a type of class that restricts which classes can inherit from it. They are used to represent restricted class hierarchies where an object can only be of one of the given types. An enum, on the other hand, is a special type that represents a group of constants (unchangeable variables).
2. Key Points
1. Flexibility: Sealed classes are more flexible than enums as they can have multiple instances with different states.
2. Inheritance: Sealed classes can be inherited by other classes, enums cannot.
3. Type of Usage: Enums are ideal for simple fixed sets of values, and sealed classes are better for complex states with varied data.
4. Storage: Enums can store properties, but all instances of an enum constant have the same property values. Sealed classes can have different properties for each instance.
3. Differences
| Characteristic | Sealed Class | Enum |
|---|---|---|
| Flexibility | Highly flexible with different states | Fixed set of constants |
| Inheritance | Can be inherited | Cannot be inherited |
| Type of Usage | Complex states with varied data | Simple fixed sets of values |
| Storage | Different properties for each instance | Same property values for all instances |
4. Example
// Example of a Sealed Class
sealed class Result {
data class Success(val message: String) : Result()
data class Error(val error: Throwable) : Result()
}
// Example of an Enum
enum class Direction {
NORTH, SOUTH, EAST, WEST
}
Output:
Sealed Class Output: Represents different types of results, each with its own state. Enum Output: Represents a fixed set of directions.
Explanation:
1. Result is a sealed class with different subclasses representing different types of results, allowing for rich state representation.
2. Direction is an enum that represents four fixed directions. Each direction is a single instance and cannot store different data per instance.
5. When to use?
- Use sealed classes when you need to represent complex states with rich information and behavior.
- Use enums when you need to define a fixed set of constants, like days of the week, states in a state machine, etc., and don't need to store varied information within each constant.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment