0
0
Kotlinprogramming~10 mins

Abstract classes and methods in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an abstract class named Vehicle.

Kotlin
abstract class [1] {
    abstract fun start()
}
Drag options to blanks, or click blank then click option'
AVehicle
BCar
CDrive
DStart
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name instead of a class name.
Forgetting to use the abstract keyword.
2fill in blank
medium

Complete the code to declare an abstract method named drive inside an abstract class.

Kotlin
abstract class Vehicle {
    abstract fun [1]()
}
Drag options to blanks, or click blank then click option'
Arun
Bdrive
Cstop
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Adding a method body to an abstract method.
Using a method name unrelated to the class.
3fill in blank
hard

Fix the error in the subclass that inherits from an abstract class and implements the abstract method.

Kotlin
abstract class Vehicle {
    abstract fun drive()
}

class Car : Vehicle() {
    override fun [1]() {
        println("Car is driving")
    }
}
Drag options to blanks, or click blank then click option'
Amove
Brun
Cstart
Ddrive
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than the abstract method.
Forgetting the override keyword.
4fill in blank
hard

Fill both blanks to create an abstract class with an abstract method and a concrete method.

Kotlin
abstract class [1] {
    abstract fun [2]()
    fun stop() {
        println("Stopping")
    }
}
Drag options to blanks, or click blank then click option'
AMachine
Bstart
Cdrive
DVehicle
Attempts:
3 left
💡 Hint
Common Mistakes
Using a concrete method name as abstract.
Naming the class with a verb instead of a noun.
5fill in blank
hard

Fill all three blanks to implement an abstract class and override its abstract method in a subclass.

Kotlin
abstract class [1] {
    abstract fun [2]()
}

class [3] : [1]() {
    override fun [2]() {
        println("Running")
    }
}
Drag options to blanks, or click blank then click option'
ARunner
Brun
CAthlete
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for class and subclass.
Not overriding the abstract method correctly.