0
0
Kotlinprogramming~5 mins

Abstract classes and methods in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Aabstract
Bopen
Cfinal
Dsealed
Can an abstract class in Kotlin have non-abstract methods?
AOnly if the methods are private
BNo, all methods must be abstract
COnly if the class is also open
DYes, it can have both abstract and non-abstract methods
What happens if a subclass does not implement all abstract methods?
AThe subclass becomes abstract automatically
BThe code compiles without errors
CThe subclass can be instantiated normally
DThe program crashes at runtime
Which of the following is true about abstract methods in Kotlin?
AThey must have a body
BThey cannot be overridden
CThey have no body and must be overridden
DThey are private by default
Can you instantiate an abstract class directly?
AYes, like any other class
BNo, you must instantiate a subclass
COnly if it has no abstract methods
DOnly inside the same package
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.