Bird
Raised Fist0
Kotlinprogramming~5 mins

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

Choose your learning style10 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:
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.

      Practice

      (1/5)
      1.

      What is the main purpose of providing a default implementation in a Kotlin interface?

      interface Example { fun greet() { println("Hello") } }

      easy
      A. To allow classes to use the function without implementing it themselves
      B. To prevent classes from overriding the function
      C. To make the interface abstract and unusable
      D. To force all classes to implement the function

      Solution

      1. Step 1: Understand default implementations in interfaces

        Default implementations provide a function body inside the interface itself.
      2. Step 2: Effect on implementing classes

        Classes can use this default function without writing their own version.
      3. Final Answer:

        To allow classes to use the function without implementing it themselves -> Option A
      4. Quick Check:

        Default implementation means no need to override [OK]
      Hint: Default means usable without override [OK]
      Common Mistakes:
      • Thinking default means function cannot be overridden
      • Confusing default implementation with abstract function
      • Believing interface becomes unusable with default methods
      2.

      Which of the following is the correct syntax for a Kotlin interface with a default implementation of a function fun info()?

      easy
      A. interface Info { fun info() = println("Info") }
      B. interface Info { fun info() println("Info") }
      C. interface Info { fun info() -> println("Info") }
      D. interface Info { fun info; println("Info") }

      Solution

      1. Step 1: Recall Kotlin interface function syntax

        Default implementations use = followed by expression or block with braces.
      2. Step 2: Check each option

        interface Info { fun info() = println("Info") } uses = with println(), which is correct syntax for default implementation.
      3. Final Answer:

        interface Info { fun info() = println("Info") } -> Option A
      4. Quick Check:

        Default function uses = expression [OK]
      Hint: Default functions use = for single expression [OK]
      Common Mistakes:
      • Using braces without = in interface default functions
      • Using arrow -> instead of =
      • Writing function body outside interface
      3.

      What will be the output of the following Kotlin code?

      interface Printer {
          fun print() { println("Printing from interface") }
      }
      
      class Document : Printer {
          override fun print() { println("Printing from Document") }
      }
      
      fun main() {
          val doc: Printer = Document()
          doc.print()
      }
      medium
      A. Printing from interface
      B. Compilation error due to override
      C. Printing from Document
      D. Runtime error

      Solution

      1. Step 1: Understand override behavior

        The class Document overrides the print() function from Printer interface.
      2. Step 2: Check which print() is called

        Even though the variable type is Printer, the actual object is Document, so overridden print() runs.
      3. Final Answer:

        Printing from Document -> Option C
      4. Quick Check:

        Overridden method runs on object type [OK]
      Hint: Overridden methods run from actual object type [OK]
      Common Mistakes:
      • Thinking interface default method runs instead of override
      • Confusing variable type with object type
      • Expecting compilation error for override
      4.

      Identify the error in this Kotlin code snippet:

      interface Logger {
          fun log() { println("Logging") }
      }
      
      class FileLogger : Logger {
          fun log() { println("File logging") }
      }
      medium
      A. FileLogger cannot implement Logger without constructor
      B. Missing override keyword in FileLogger's log() function
      C. Interface functions cannot have bodies
      D. log() function must be abstract in interface

      Solution

      1. Step 1: Check interface function declaration

        Logger interface has a default implementation of log().
      2. Step 2: Check FileLogger's log() function

        It defines log() but misses the override keyword, which is required.
      3. Final Answer:

        Missing override keyword in FileLogger's log() function -> Option B
      4. Quick Check:

        Override keyword required when overriding [OK]
      Hint: Override keyword is mandatory for overrides [OK]
      Common Mistakes:
      • Forgetting override keyword
      • Thinking interface functions cannot have bodies
      • Assuming abstract keyword needed in interface
      5.

      Given this Kotlin interface and classes, what will be the output when main() runs?

      interface Speaker {
          fun speak() { println("Speaking from Speaker") }
      }
      
      class Person : Speaker
      
      class Robot : Speaker {
          override fun speak() { println("Speaking from Robot") }
      }
      
      fun main() {
          val list: List<Speaker> = listOf(Person(), Robot())
          for (item in list) {
              item.speak()
          }
      }
      hard
      A. Compilation error due to missing override in Person
      B. Speaking from Person Speaking from Robot
      C. Speaking from Speaker Speaking from Speaker
      D. Speaking from Speaker Speaking from Robot

      Solution

      1. Step 1: Analyze Person class

        Person does not override speak(), so it uses Speaker's default implementation.
      2. Step 2: Analyze Robot class

        Robot overrides speak(), so its own version runs.
      3. Step 3: Loop output

        First item (Person) prints "Speaking from Speaker", second (Robot) prints "Speaking from Robot".
      4. Final Answer:

        Speaking from Speaker Speaking from Robot -> Option D
      5. Quick Check:

        Default + override methods run accordingly [OK]
      Hint: Classes use default unless overridden [OK]
      Common Mistakes:
      • Expecting Person to print "Speaking from Person"
      • Thinking missing override causes error
      • Assuming both print Robot's message
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard