0
0
Kotlinprogramming~5 mins

Interface declaration and implementation in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an interface in Kotlin?
An interface in Kotlin is a contract that defines a set of functions and properties that a class can implement. It specifies what a class should do, but not how to do it.
Click to reveal answer
beginner
How do you declare an interface in Kotlin?
Use the interface keyword followed by the interface name and curly braces. Inside, define abstract functions or properties. Example: <br>
interface Drivable { fun drive() }
Click to reveal answer
beginner
How does a class implement an interface in Kotlin?
A class uses a colon <code>:</code> followed by the interface name to implement it. The class must provide concrete implementations for all interface functions. Example: <br><pre>class Car : Drivable { override fun drive() { println("Driving") } }</pre>
Click to reveal answer
intermediate
Can interfaces in Kotlin have method implementations?
Yes, interfaces can have default method implementations. Classes implementing the interface can use these defaults or override them with their own versions.
Click to reveal answer
intermediate
What happens if a class does not implement all interface methods?
The Kotlin compiler will show an error. The class must implement all abstract methods of the interface unless the class is declared abstract.
Click to reveal answer
How do you declare an interface named Flyable in Kotlin?
Aclass Flyable { fun fly() }
Binterface Flyable() { fly() }
Cfun interface Flyable { fly() }
Dinterface Flyable { fun fly() }
What keyword does a Kotlin class use to implement an interface?
Aextends
Bimplements
C:
Dinherits
Can Kotlin interfaces contain properties?
AYes, but they must be abstract or have default getters
BNo, only functions
CYes, with backing fields
DNo, only constants
What must a class do when it implements an interface with abstract methods?
AImplement all abstract methods or be declared abstract
BImplement only some methods
CNothing, methods are optional
DOverride only default methods
If an interface method has a default implementation, can a class override it?
ANo, default implementations cannot be changed
BYes, the class can override it
COnly if the method is marked open
DOnly if the class is abstract
Explain how to declare and implement an interface in Kotlin with a simple example.
Think about how you tell a class what it must do using an interface.
You got /5 concepts.
    Describe what happens if a Kotlin class does not implement all abstract methods of an interface it declares to implement.
    Consider what the compiler expects from a class that promises to follow an interface.
    You got /3 concepts.