Member-only story

Sealed Classes vs Sealed Interfaces in Kotlin: What’s the Difference?

PINAR TURGUT
6 min readApr 20, 2023

--

Are you curious about Kotlin, the cool programming language that all the hip developers are using these days? Well, let me tell you about sealed classes and sealed interfaces, two fancy terms that might sound like something you’d find in a secret spy organization, but are actually just ways to make your code more organized and predictable.

Let’s start with sealed classes. Imagine you have a set of different animals, like dogs, cats, and monkeys. You could represent these animals as a sealed class, with each subtype representing a specific animal. This way, you know exactly what kinds of animals you’re dealing with, and you can control their behavior in a more structured way. Think of it like a fancy zoo with clearly labeled enclosures, instead of a chaotic jungle where anything could happen.

Now, let’s talk about sealed interfaces. Imagine you have a set of different superheroes, like Superman, Spider-Man, and Wonder Woman. You could represent their powers as a sealed interface, with each implementation representing a specific power. This way, you know exactly what kinds of powers your superheroes have, and you can use them in a more organized way. Think of it like a superhero team where everyone has a specific role and power, instead of a random group of people with no clear direction.

--

--

PINAR TURGUT
PINAR TURGUT

Written by PINAR TURGUT

Android developers who is passionate about personal growth https://pinartechtips.com

Responses (3)