Interfaces and Abstract Classes in Kotlin: Getting Started

Learn how to best use interfaces and abstract classes to create class hierarchies in your Kotlin Android apps. By Mattia Ferigutti.

4.5 (4) · 1 Review

Download materials
Save for later
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Abstract Classes vs Interfaces

What’s the difference between abstract classes and interfaces? This is one of the most asked questions during coding interviews. You’ve already learned many things about interfaces and abstract classes, but here are some more thoughts you can use in your future interviews.

Both abstract classes and interfaces:

  • Can’t be instantiated.
  • Define both abstract and regular members.
  • Define static members.
  • Force subclasses to implement a behavior.

However, interfaces don’t have a constructor, and you can implement them multiple times.

From a features and characteristics point of view, these two tools are very similar. The thing that makes them distinct is the conceptual difference.

When you need to define a too-generic abstraction, it’s time to use an abstract class. You can see them as templates that subclasses can extend.

For example, in the Animal class, food is too general to be defined directly in the parent class. But any animal that extends Animal must implement it and return the food the animal eats.

An interface abstracts a specific behavior that multiple classes can implement. For example, Walkable implements a specific behavior for animals that can walk or run.

Where to Go From Here?

Download the completed project files by clicking Download Materials at the top or bottom of the tutorial.

Great job on making it all the way through! You’ve learned the difference between an abstract class and an interface and when to use them. However, there’s another approach to handling this kind of relationship between objects called composition.

Interfaces are the base for composition, and this article hopefully helped you understand them. If you want to find out more about composition, check out this Using Composition in Kotlin tutorial.

If you have any questions or comments, please join the forum discussion below!