Recall & Review
beginner
What is an abstract class in Kotlin?An abstract class in Kotlin is a class that cannot be instantiated directly. It is designed to be a base class for other classes and can contain abstract methods that must be implemented by subclasses.Click to reveal answer
beginner
What is an abstract method in Kotlin?
An abstract method is a method declared inside an abstract class without a body. Subclasses must provide an implementation for this method.Click to reveal answer
beginner
Can you create an instance of an abstract class directly in Kotlin?No, you cannot create an instance of an abstract class directly. You must create a subclass that implements all abstract methods and then instantiate that subclass.Click to reveal answer
beginner
How do you declare an abstract class and an abstract method in Kotlin? Provide a simple example.Use the keyword 'abstract' before the class and method. Example:<br><pre>abstract class Animal {
abstract fun sound()
}
class Dog : Animal() {
override fun sound() {
println("Woof")
}
}</pre>Click to reveal answer
intermediate
Why use abstract classes and methods?
Abstract classes and methods help define a common template for related classes. They enforce that subclasses implement specific behaviors, promoting code organization and reuse.
Click to reveal answer
Which keyword is used to declare an abstract class in Kotlin?
✗ Incorrect
The keyword 'abstract' is used to declare an abstract class in Kotlin.
Can an abstract class in Kotlin have non-abstract methods?
✗ Incorrect
An abstract class can have both abstract methods (without body) and non-abstract methods (with body).
What happens if a subclass does not implement all abstract methods?
✗ Incorrect
If a subclass does not implement all abstract methods, it must be declared abstract itself.
Which of the following is true about abstract methods in Kotlin?
✗ Incorrect
Abstract methods have no body and must be overridden in subclasses.
Can you instantiate an abstract class directly?
✗ Incorrect
Abstract classes cannot be instantiated directly; you must instantiate a subclass that implements all abstract methods.
Explain what an abstract class and an abstract method are in Kotlin and why they are useful.
Think about how abstract classes help organize code and enforce rules for subclasses.
You got /4 concepts.
Write a simple Kotlin example showing an abstract class with one abstract method and a subclass that implements it.
Use 'abstract' keyword and 'override' keyword.
You got /4 concepts.