Exploring Types of Classes in Kotlin: A Brief Guide
Kotlin Miscellaneous: Classes in kotlin

Hello there ✋🏻, In my previous article, we thoroughly discussed nested and inner classes. although, Kotlin has several other types of classes as well that we will explore in this article.
Regular Class:
A regular class is like a template or a blueprint to create an object. Imagine you want to build a car. You start with a plan that describes what the car will look like and how it will function. In programming, this plan is called a class.
class Car(val brand: String, val model: String) {
fun drive() {
println("Driving a $brand $model")
}
}
fun main() {
val car = Car("Toyota", "Corolla")
car.drive()
}
Abstract Class:
An abstract class is like a partially completed blueprint. It provides some details but leaves some parts for others to fill in. You can’t create an object from an abstract class directly.
abstract class Animal(val name: String) {
abstract fun makeSound()
}
class Dog(name: String) : Animal(name) {
override fun makeSound() {
println("$name says Woof!")
}
}
fun main() {
val dog = Dog("Buddy")
dog.makeSound()
}
Nested Class and Inner Class:
We have already discussed about them in previous article.
Sealed Class:
A sealed class restricts the types that can inherit from it. Think of it as a closed set of blueprints where only certain specific types can be created.
sealed class Result {
class Success(val data: String) : Result()
class Error(val error: String) : Result()
}
fun handleResult(result: Result) {
when (result) {
is Result.Success -> println("Success: ${result.data}")
is Result.Error -> println("Error: ${result.error}")
}
}
fun main() {
val result: Result = Result.Success("Operation completed successfully")
handleResult(result)
}
Data Class:
A data class is like a regular class but specifically designed to hold data. Think of it as a special type of class that focuses on storing information. They automatically generate useful methods like equals()
, hashCode()
, and toString()
.
data class User(val name: String, val age: Int)
fun main() {
val user1 = User("Alice", 25)
val user2 = User("Alice", 25)
println(user1) // Prints: User(name=Alice, age=25)
println(user1 == user2) // Prints: true
}
Annotation Class:
An annotation class in Kotlin is used to define a new annotation type. Annotations are essentially markers or labels that you can place on your code to give it additional information.
annotation class Fancy(val author: String)
@Fancy(author = "Samir Shaikh")
class MyClass
enum Class:
Enum classes are used to represent a fixed set of constants.
enum class Direction {
NORTH, SOUTH, EAST, WEST
}
fun main() {
val direction = Direction.NORTH
when (direction) {
Direction.NORTH -> println("Going North")
Direction.SOUTH -> println("Going South")
Direction.EAST -> println("Going East")
Direction.WEST -> println("Going West")
}
}
I hope 🤞🏻 you enjoyed reading my article and learned something new. If you like this article give it a clap and share it with your android buddies. Also, if you have any suggestions or want me to cover any topic, feel free to drop a comment.
Thank you!✌️