Interfaces & Abstraction

Abstract classes, interfaces, implementation, multiple interfaces, and visibility modifiers

abstract interface Multiple Visibility

Table of Contents

1Abstract Classes

An abstract class cannot be instantiated directly. It may contain abstract members (without implementation) and concrete members (with bodies). Subclasses must implement all abstract members or be abstract themselves.

Abstract class exampleabstract class Animal(val name: String) { abstract fun makeSound() fun sleep() { println("$name is sleeping") } } class Dog(name: String) : Animal(name) { override fun makeSound() { println("$name says Woof!") } } fun main() { val dog = Dog("Buddy") dog.makeSound() dog.sleep() }

Abstract class vs regular class

FeatureAbstract classRegular class
InstantiationCannot create objects directlyCan create objects
Abstract membersAllowed (abstract fun)Not allowed
State (properties)Can hold mutable stateCan hold state
InheritanceSingle class inheritance onlySingle class inheritance only

Abstract properties

Abstract propertyabstract class Shape { abstract val name: String abstract fun area(): Double } class Circle(val radius: Double) : Shape() { override val name = "Circle" override fun area() = Math.PI * radius * radius }
When to use abstract class

Use when subclasses share common state or base behavior — e.g. a BaseFragment with shared lifecycle helpers, or a BaseViewModel with common loading state.

2Interfaces

An interface defines a contract — a set of methods and properties that implementing classes must provide. Kotlin interfaces can contain abstract members, default implementations, and properties.

Basic interfaceinterface Clickable { fun click() fun showPointer() { println("Pointer visible") // default implementation } }

Interface properties

Property in interfaceinterface UserRepository { val isConnected: Boolean fun getUser(id: Int): String }

Abstract class vs interface

AspectAbstract classInterface
Inheritance countOne class onlyMultiple interfaces allowed
ConstructorHas constructor parametersNo constructor
State storageCan store state in propertiesProperties usually abstract or without backing field
Default methodsConcrete methods in class bodyDefault implementations in interface body
Best forShared base + stateCapabilities, contracts, APIs
Functional interface (SAM)fun interface OnClickListener { fun onClick() } fun main() { val listener = OnClickListener { println("Clicked!") } listener.onClick() }
fun interface: A functional interface (Single Abstract Method) can be implemented with a lambda — common in Android callbacks and Compose event handlers.

3Interface Implementation

A class implements an interface using the same : syntax as inheritance. All abstract members must be overridden unless the class is abstract.

Implementing an interfaceinterface Drawable { fun draw() } class Button(val label: String) : Drawable { override fun draw() { println("Drawing button: $label") } } fun main() { val btn: Drawable = Button("Submit") btn.draw() }

Class extending base + implementing interface

Inheritance + interfaceopen class Vehicle(val brand: String) interface Electric { fun charge() } class Tesla(brand: String) : Vehicle(brand), Electric { override fun charge() { println("$brand is charging...") } } fun main() { Tesla("Model 3").charge() }

Overriding default interface methods

Override default implementationinterface Logger { fun log(message: String) { println("[LOG] $message") } } class FileLogger : Logger { override fun log(message: String) { println("[FILE] $message") } } fun main() { FileLogger().log("Saved") }

Implementing interface properties

Interface property implementationinterface Configurable { val configName: String } class AppConfig : Configurable { override val configName: String = "production" }
Program to the interface

Declare variables as interface types — val repo: UserRepository = RemoteUserRepository() — so implementations can be swapped (e.g. for testing with a fake repository).

4Multiple Interfaces

Kotlin supports multiple interface inheritance — a class can implement several interfaces by listing them after the superclass, separated by commas. Kotlin does not support multiple class inheritance.

Multiple interfacesinterface Flyable { fun fly() } interface Swimmable { fun swim() } class Duck : Flyable, Swimmable { override fun fly() = println("Duck flying") override fun swim() = println("Duck swimming") } fun main() { val duck = Duck() duck.fly() duck.swim() }

Class + multiple interfaces

One class, two interfacesopen class Employee(val name: String) interface Payable { fun pay(amount: Double) } interface Reportable { fun generateReport(): String } class Manager(name: String) : Employee(name), Payable, Reportable { override fun pay(amount: Double) { println("Paid $name: $$amount") } override fun generateReport(): String { return "Report for $name" } }

Resolving conflicting defaults

When two interfaces provide the same default method, the class must override it and choose which implementation to use:

Conflict resolution with super<Interface>interface A { fun greet() = println("Hello from A") } interface B { fun greet() = println("Hello from B") } class C : A, B { override fun greet() { super<A>.greet() super<B>.greet() println("Hello from C") } } fun main() { C().greet() }
AllowedNot allowed
class X : Base(), I1, I2, I3Extending two classes
Interface extending multiple interfacesMultiple class inheritance
interface I : I1, I2
Android example: An Activity implements lifecycle interfaces implicitly; custom classes often implement Serializable, Parcelable, or domain interfaces like Repository alongside a base class.

5Visibility Modifiers

Visibility modifiers control who can access classes, interfaces, and their members. Kotlin's modifiers apply to declarations at class, function, and property level.

ModifierClass memberTop-level declaration
publicVisible everywhereVisible everywhere (default)
privateVisible inside the declaring class onlyVisible in the same file only
protectedVisible in the class and subclassesNot allowed at top level
internalVisible within the same moduleVisible within the same module

Visibility on classes and interfaces

Visibility examplespublic class PublicApi // default — visible everywhere internal class ModuleHelper // visible only in this module private class InternalUtil // visible only in this file interface Repository { fun fetch(id: Int): String // public by default private fun validate(id: Int) { // private helper inside interface require(id > 0) } }

Protected in inheritance

protected membersopen class Base { protected open fun onInit() { println("Base init") } } class Derived : Base() { override fun onInit() { super.onInit() println("Derived init") } fun start() = onInit() // OK — same class hierarchy }

Visibility on interface implementations

Implementations cannot reduce visibilityinterface Service { fun execute() } class MyService : Service { // Must be public — cannot make interface method private override fun execute() { println("Executing...") } internal fun helper() { } // internal is OK for new methods }

Module vs package

internal

Hides API within a Gradle module — ideal for implementation details in libraries.

private (top-level)

File-scoped — helpers in the same .kt file only.

protected

Exposed to subclasses but hidden from unrelated classes.

public

Default for published APIs — use intentionally for library surface.

API design

Expose interfaces as public, keep implementations internal where possible — callers depend on the contract, not the concrete class. Related: Encapsulation in OOP.

6Summary Cheatsheet

TopicKey Takeaway
Abstract Classesabstract class; abstract + concrete members; single inheritance
InterfacesContract with abstract/default members; no constructor
Implementationclass X : Base(), Interface { override }
Multiple InterfacesComma-separated; resolve conflicts with super<I>
Visibilitypublic, private, protected, internal
fun interfaceSAM — implement with lambda
Next lessonData, Enum & Sealed Classes

Kotlin Interfaces & Abstraction MCQ Practice

10 Basic MCQs 10 Advanced MCQs

10 Basic Kotlin Interfaces & Abstraction MCQs

1

Interfaces are declared with?

Aabstract interface
Binterface
Cprotocol
Dtrait
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is A.
2

A class can implement how many interfaces?

AOne only
BMultiple
CNone
DOnly with abstract class
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is A.
3

Implementation syntax?

A: InterfaceName
Bimplements InterfaceName
Cextends InterfaceName
Dusing InterfaceName
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is B.
4

Abstract classes can have?

AOnly abstract methods
BAbstract and concrete members with state
CNo constructors
DOnly static methods
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is A.
5

Interface methods with body are?

AIllegal
BDefault implementations allowed
COnly in Java
DAlways private
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is B.
6

abstract keyword on class means?

ACan instantiate
BCannot instantiate; may have abstract members
CSame as interface
DSingleton
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is A.
7

override is required when?

AImplementing interface methods
BNever
COnly for private
DOnly in companion
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is A.
8

Abstraction hides?

ASyntax
BImplementation details behind a contract
COnly variables
DPackages
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is A.
9

Interface can extend?

AMultiple classes
BOther interfaces
COnly Object
DNothing
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is B.
10

Visibility of interface members default?

Aprivate
Bpublic
Cprotected
Dinternal only
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is A.

10 Advanced Kotlin Interfaces & Abstraction MCQs

11

Delegation syntax class X : I by helper means?

AMultiple inheritance of state
BForward interface calls to helper object
CDelete interface
DAbstract only
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is B.
12

SAM conversion allows?

ALambda where single abstract method interface expected
BMultiple abstract methods
COnly enums
DOnly sealed
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is A.
13

abstract fun in abstract class?

AMust have body
BNo body — subclass implements
COnly static
DPrivate only
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is B.
14

Interface can declare properties?

ANever
BYes — abstract or with getter implementation
COnly val in Java
DOnly var
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is A.
15

Diamond problem with multiple interfaces?

AKotlin allows multiple class inheritance
BResolved by explicit override if conflicts
CAlways crash
DNot applicable — no state in interfaces typically
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is B.
16

sealed interface restricts?

AOnly enums
BImplementations to known set in module
CAll classes
DNothing
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is A.
17

abstract class constructor?

ANot allowed
BAllowed
COnly private
DOnly in interface
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is B.
18

Functional interface in Kotlin?

Afun interface with one abstract method
BAny interface
COnly Runnable
Ddata interface
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is A.
19

Interface vs abstract class for API?

AInterface for capability contracts; abstract for shared base state
BAlways abstract
CAlways interface
DSame thing
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is A.
20

internal visibility means?

AVisible everywhere
BVisible within same module
CPrivate to file
DPublic in Java only
Explanation: See Kotlin Interfaces & Abstraction lesson — correct answer is A.
Click an option to select, then check answers.

Kotlin Interfaces & Abstraction Interview Q&A

15 topic-focused questions for interviews and revision.

1What is an interface in Kotlin?easy
Answer: A contract defining methods and properties that implementing classes must provide. Declared with interface Keyword.
2How do you implement an interface?easy
Answer: class MyClass : MyInterface { override fun method() { } } — override required for each member.
3Can a class implement multiple interfaces?easy
Answer: Yes — class Demo : InterfaceA, InterfaceB { ... } unlike single class inheritance.
4What is an abstract class?easy
Answer: A class that cannot be instantiated; may have abstract members without body and concrete members with state.
5Interface default implementation?medium
Answer: interface I { fun greet() = "Hello" } — implementing class inherits default unless overridden.
6Difference between abstract class and interface?medium
Answer: Abstract: constructors, state, single inheritance. Interface: multiple implementation, no constructor, contract-focused.
7What is abstraction?medium
Answer: Hiding implementation details and exposing only what callers need through abstract types or interfaces.
8Explain visibility modifiers in Kotlin.medium
Answer: public (default), private, protected (subclasses), internal (module). Control API surface of classes and interfaces.
9What is delegation by?medium
Answer: class Printer : Output by delegate — forwards interface methods to delegate object; reduces boilerplate.
10What is a SAM interface?medium
Answer: fun interface OnClick { fun onClick() } — lambda can implement single abstract method interfaces.
11How resolve conflicting default methods from two interfaces?hard
Answer: Override explicitly in the implementing class and choose which super to call: InterfaceA.foo() or InterfaceB.foo().
12When choose abstract class over interface?hard
Answer: When subclasses share common state, initialization, or non-trivial default behavior in a base class.
13Can interfaces have properties?hard
Answer: Yes — abstract val/getter or property with default getter implementation; implementing class provides storage or override.
14sealed interface use case?hard
Answer: Restrict implementations to known set for exhaustive when — e.g. UI events or network result types across modules.
15How does Kotlin avoid Java diamond problem?hard
Answer: No multiple class inheritance; interfaces typically stateless; explicit override resolves default method conflicts.