Inheritance & Interfaces

Kotlin is a modern, concise language that supports object-oriented design through inheritance and interfaces — but with safe defaults, since classes are final unless you opt in with open .

Learn Inheritance & Interfaces in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll build class hierarchies, override methods polymorphically, and implement interfaces and abstract classes.

What You'll Learn in This Lesson

1️⃣ Inheritance and Overriding

In Kotlin, classes and methods are final by default . To allow a subclass, mark the class open ; to allow overriding a method, mark it open too. A subclass inherits with : and replaces behaviour with override .

This is polymorphism : the loop calls describe() on each animal, and the correct speak() runs depending on the actual type.

2️⃣ Interfaces and Abstract Classes

An interface defines a contract and may include default implementations. An abstract class can hold state and mix concrete with abstract members. A class can implement many interfaces but extend only one class.

Car extends an abstract Vehicle and implements Drivable — and gets honk() free from the interface's default.

Your turn. Replace the TODO , then run and compare.

Define one interface and two implementations, then treat them polymorphically through the interface type.

📋 Quick Reference — Inheritance

Practice quiz

By default, a Kotlin class is…

  • open for subclassing
  • abstract
  • final and cannot be inherited
  • an interface

Answer: final and cannot be inherited. Classes are final by default; you must mark them open to allow subclassing.

Which keyword allows a class or method to be inherited or overridden?

  • open
  • public
  • virtual
  • extend

Answer: open. open allows a class to be subclassed and a method to be overridden.

What keyword must a subclass put before a method that replaces a base method?

  • replace
  • super
  • override
  • open

Answer: override. Replacing an inherited method requires the override keyword.

How does a subclass declare it inherits from a base class?

  • With the keyword extends
  • With a colon, e.g. class Dog : Animal()
  • With the keyword implements
  • With the keyword inherits

Answer: With a colon, e.g. class Dog : Animal(). Kotlin uses a colon: class Dog(name: String) : Animal(name).

What does polymorphism mean in the Animal example?

  • All animals share one speak()
  • The correct overridden speak() is chosen at runtime
  • Animals cannot be put in a list
  • speak() must be final

Answer: The correct overridden speak() is chosen at runtime. Calling speak() runs the right override for each object's actual type at runtime.

Which statement about interfaces is true?

  • They can store mutable state in backing fields
  • A class can implement only one interface
  • They can include default method implementations
  • They cannot declare properties

Answer: They can include default method implementations. Interface methods may provide a default body that implementers inherit.

How many classes can a Kotlin class extend?

  • Exactly one
  • As many as needed
  • None
  • Up to two

Answer: Exactly one. A class can extend only one class but implement many interfaces.

What can an abstract class do that an interface cannot?

  • Declare abstract methods
  • Be implemented by a class
  • Hold state in backing fields with a constructor
  • Have a name

Answer: Hold state in backing fields with a constructor. An abstract class can hold state and have a constructor; interfaces cannot store fields.

How do you call the parent's version of a method?

  • parent.method()
  • base.method()
  • this.method()
  • super.method()

Answer: super.method(). super.method() invokes the superclass implementation.

When a Car implements Drivable, where does honk() come from if Car doesn't define it?

  • The interface's default implementation
  • A compile error
  • The Vehicle class
  • It returns null

Answer: The interface's default implementation. Car inherits honk() free from Drivable's default implementation.