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?✗ Incorrect
The correct syntax uses the
interface keyword followed by the name and curly braces with function declarations.What keyword does a Kotlin class use to implement an interface?
✗ Incorrect
Kotlin uses a colon
: to indicate implementation or inheritance.Can Kotlin interfaces contain properties?
✗ Incorrect
Interfaces can declare properties but they cannot have backing fields. They can have abstract properties or properties with default getters.
What must a class do when it implements an interface with abstract methods?
✗ Incorrect
The class must implement all abstract methods or be declared abstract itself.
If an interface method has a default implementation, can a class override it?
✗ Incorrect
Classes can override default interface methods to provide their own implementation.
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.