0
0
Kotlinprogramming~5 mins

Interface with default implementations in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an interface with default implementations in Kotlin?
An interface in Kotlin can have methods with default implementations. This means you can define a function inside an interface with a body, so classes implementing the interface don't have to provide their own version unless they want to override it.
Click to reveal answer
beginner
How do you declare a function with a default implementation inside a Kotlin interface?
Inside the interface, you write the function with its body, like this:<br>
interface MyInterface {
  fun greet() {
    println("Hello from interface")
  }
}
Click to reveal answer
beginner
Can a class that implements an interface with default implementations choose to override those methods?
Yes. The class can use the default implementation or provide its own version by overriding the method.
Click to reveal answer
intermediate
Why are default implementations in interfaces useful?
They let you add new functions to interfaces without breaking existing classes. Classes can use the default code or customize it, making code easier to maintain and extend.
Click to reveal answer
intermediate
What happens if two interfaces with the same default method are implemented by a class?
The class must override the conflicting method and provide its own implementation to resolve the conflict.
Click to reveal answer
In Kotlin, how do you provide a default implementation for a method in an interface?
ABy writing the method with a body inside the interface
BBy using the 'default' keyword before the method
CBy implementing the method in the class only
DBy declaring the method as abstract
If a class implements an interface with a default method, what can it do?
AIgnore the method completely
BOverride the method to provide its own implementation or use the default
COnly use the default implementation without changes
DMust always override the method
What must a class do if it implements two interfaces that have the same default method?
AOverride the method to resolve the conflict
BUse the default method from the second interface
CUse the default method from the first interface
DThe compiler automatically merges the methods
Why are default implementations in interfaces helpful?
AThey force all classes to implement the method
BThey make interfaces slower
CThey allow adding new methods without breaking existing code
DThey remove the need for classes
Which keyword is NOT used when defining default implementations in Kotlin interfaces?
Aoverride
Bfun
Cinterface
Ddefault
Explain how Kotlin interfaces support default implementations and why this feature is useful.
Think about how interfaces can have code inside them and how classes can use or change that code.
You got /4 concepts.
    Describe what happens when a class implements two interfaces that have the same default method.
    Consider how Kotlin handles multiple inheritance of behavior.
    You got /3 concepts.