Chapter 13: Interfaces

Interfaces — this is one of the most powerful and most used features in Kotlin!

In Kotlin, interfaces are much more expressive than in Java — especially since Kotlin 1.0 and with the improvements in later versions. You’ll love how clean, flexible, and safe they are.

We’re going to go super slowly, like we’re sitting together in a quiet Mumbai café — I’ll explain every single concept with real-life analogies, complete runnable programs, step-by-step breakdowns, tables, common mistakes with fixes, and tons of examples you can copy-paste and run right now.

Let’s dive in! ☕🚀

1. Interface Declaration & Implementation

Interface in Kotlin is a contract — it defines what a class must do, but not how.

Syntax (very clean):

Kotlin

Implementing an interface (using 🙂

Kotlin

Key points:

  • No implements keyword — just : (same as inheritance)
  • All methods in interface are abstract by default — no need to write abstract
  • All properties in interface are abstract too (no backing field)

Example – Multiple methods

Kotlin

2. Default Implementations in Interfaces

Since Kotlin 1.0 (and improved in later versions), interfaces can have default method implementations — just like Java 8+.

This is huge — you can evolve interfaces without breaking existing code!

Kotlin

Overriding default implementation (optional)

Kotlin

3. Functional Interfaces (fun interface)

A functional interface is an interface with exactly one abstract method — perfect target for lambdas!

Kotlin gives you a special marker: fun interface

Kotlin

Using it with lambda

Kotlin

Why use fun interface?

  • Makes it explicit that this interface is meant for lambdas
  • Allows SAM conversion (Single Abstract Method) — lambda becomes instance automatically
  • Very common in Android (listeners), callbacks, etc.

Example – Real-world Android-style listener

Kotlin

4. Multiple Inheritance with Interfaces

Kotlin fully supports multiple inheritance through interfaces (but not classes — only one parent class).

A class can implement any number of interfaces.

Example – Multiple inheritance

Kotlin

Output:

text

Resolving conflicts (if two interfaces have same method):

Kotlin

Quick Recap Table (Your Cheat Sheet)

Feature Kotlin Way (Best Practice) Key Point
Interface declaration interface Flyable { fun fly() } No abstract needed
Default implementation fun log() { println(“…”) } Safe to add without breaking code
fun interface fun interface OnClick { fun onClick() } Perfect for lambdas
Multiple inheritance class Duck : Flyable, Swimmable Allowed with interfaces
Call specific super super<Flyable>.fly() Resolves conflicts

Common Newbie Mistakes & Fixes

Mistake Problem Fix
Forgetting override when implementing Compile error Always write override for abstract methods
Not marking interface method as open Cannot override No need — all interface methods are open
Using class instead of interface Cannot implement multiple Use interface for contracts
Forgetting fun interface for lambdas Lambda conversion not allowed Add fun interface
Not handling conflicts in multiple inheritance Compile error Override and use super<Interface>.method()

Homework for You (Let’s Make It Fun!)

  1. Basic Create interface Printable with method printDetails(). Implement it in Book and Movie classes.
  2. Medium Create interface Logger with default logInfo() and logError(). Implement it in ConsoleLogger that overrides logError().
  3. Advanced Create two interfaces Eatable and Flyable. Make Bat implement both → override fly() to call both defaults.
  4. Fun Create fun interface OnLoginListener → use lambda to handle login success/failure.
  5. Challenge Create interface Resizable with default resize(factor: Double) → implement in Circle and Rectangle.

You’ve just mastered Kotlin’s powerful interface system — now you can design clean, flexible, and reusable contracts!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *