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
✗ Incorrect
The keyword 'abstract' is used to declare an abstract class in Kotlin.
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
✗ 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?
AThe subclass becomes abstract automatically
BThe code compiles without errors
CThe subclass can be instantiated normally
DThe program crashes at runtime
✗ 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?
AThey must have a body
BThey cannot be overridden
CThey have no body and must be overridden
DThey are private by default
✗ Incorrect
Abstract methods have no body and must be overridden in subclasses.
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
✗ 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.
Practice
(1/5)
1. What is true about an abstract class in Kotlin?
easy
A. It cannot be instantiated directly and may contain abstract methods.
B. It can be instantiated like any other class.
C. It must not contain any methods with a body.
D. It is the same as an interface.
Solution
Step 1: Understand abstract class instantiation rules
Abstract classes cannot create objects directly because they are incomplete by design.
Step 2: Check method types in abstract classes
Abstract classes can have both abstract methods (without body) and concrete methods (with body).
Final Answer:
It cannot be instantiated directly and may contain abstract methods. -> Option A
Quick Check:
Abstract class = no direct objects + can have abstract methods [OK]
Believing abstract classes can't have method bodies
Confusing abstract classes with interfaces
2. Which of the following is the correct way to declare an abstract method in a Kotlin abstract class?
easy
A. abstract fun calculate(): Int
B. fun abstract calculate(): Int
C. fun calculate() abstract: Int
D. abstract calculate fun(): Int
Solution
Step 1: Recall Kotlin syntax for abstract methods
In Kotlin, abstract methods are declared with the abstract keyword before fun, followed by the method signature without a body.
Step 2: Check each option's syntax
Only abstract fun calculate(): Int matches the correct syntax: abstract fun calculate(): Int. Others have incorrect keyword order or punctuation.
Final Answer:
abstract fun calculate(): Int -> Option A
Quick Check:
abstract fun method() = correct syntax [OK]
Hint: abstract keyword comes before fun in method declaration [OK]
Common Mistakes:
Placing abstract after fun
Adding a method body to abstract methods
Incorrect keyword order
3. What will be the output of this Kotlin code?
abstract class Animal {
abstract fun sound(): String
}
class Dog : Animal() {
override fun sound() = "Bark"
}
fun main() {
val dog = Dog()
println(dog.sound())
}
medium
A. Animal
B. sound
C. Bark
D. Compilation error
Solution
Step 1: Understand class inheritance and method override
The abstract class Animal has an abstract method sound(). The subclass Dog overrides it to return "Bark".
Step 2: Analyze main function output
Creating a Dog object and calling sound() prints "Bark".
Final Answer:
Bark -> Option C
Quick Check:
Overridden method returns "Bark" [OK]
Hint: Overridden abstract method returns subclass value [OK]
Common Mistakes:
Expecting abstract class method to run
Confusing method name with output
Thinking abstract class can be instantiated
4. Identify the error in this Kotlin code:
abstract class Vehicle {
abstract fun drive()
}
class Car : Vehicle() {
fun drive() {
println("Driving")
}
}
medium
A. drive method in Vehicle should have a body
B. Vehicle class cannot be abstract
C. Car class cannot inherit from Vehicle
D. Missing override keyword in Car's drive method
Solution
Step 1: Check method overriding rules in Kotlin
When a subclass implements an abstract method, it must use the override keyword.
Step 2: Identify missing override keyword
The Car class defines drive() without override, causing a compilation error.
Final Answer:
Missing override keyword in Car's drive method -> Option D
Quick Check:
Override keyword required for abstract method implementation [OK]
Hint: Use override keyword when implementing abstract methods [OK]
Common Mistakes:
Omitting override keyword
Adding body to abstract method in base class
Misunderstanding inheritance rules
5. Given this abstract class and subclass, what will be the output?
abstract class Shape {
abstract fun area(): Double
fun description() = "Shape area is ${area()}"
}
class Circle(val radius: Double) : Shape() {
override fun area() = 3.14 * radius * radius
}
fun main() {
val c = Circle(2.0)
println(c.description())
}
hard
A. Shape area is 6.28
B. Shape area is 12.56
C. Shape area is 4.0
D. Compilation error due to abstract method
Solution
Step 1: Calculate area in Circle subclass
Area = π * radius² = 3.14 * 2.0 * 2.0 = 12.56
Step 2: Call description method from Shape
Description returns string with area value: "Shape area is 12.56".