What if you could write one plan and have many different characters follow it perfectly without confusion?
Why Interface declaration and implementation in Kotlin? - Purpose & Use Cases
Imagine you are building a game where different characters can perform actions like jump, run, or attack. Without a clear plan, you write separate code for each character, repeating similar actions again and again.
This manual way is slow and confusing. If you want to add a new character or change an action, you must update many places. Mistakes happen easily, and your code becomes messy and hard to fix.
Interfaces let you define a clear list of actions that any character must have. You write the action rules once, then each character promises to follow them. This keeps your code neat, easy to update, and everyone knows what to expect.
class Knight { fun attack() { /* code */ } } class Archer { fun attack() { /* code */ } }
interface Fighter {
fun attack()
}
class Knight : Fighter {
override fun attack() { /* code */ }
}
class Archer : Fighter {
override fun attack() { /* code */ }
}Interfaces make your code flexible and organized, so you can add new features easily without breaking what already works.
Think of a remote control that works with many devices. The remote defines buttons (interface), and each device implements how those buttons work, so you can control TVs, music players, or lights with the same remote.
Interfaces define a clear set of actions to follow.
They help avoid repeating code and reduce errors.
They make adding new features easier and safer.