Abstract Classes in Kotlin
Abstract classes are classes that cannot be instantiated directly and are meant to be subclassed.
They can contain both abstract members (without implementation) and concrete members (with implementation).
Abstract classes are used to define a common blueprint for multiple subclasses.
1. Why Abstract Classes Matter
- Define a common blueprint: Abstract classes provide a structure that multiple subclasses can follow.
- Partial implementation: Allows shared functionality while leaving some methods abstract for subclasses to implement.
- Enforce contracts: Ensures that certain methods are implemented by all subclasses.
- Polymorphism: Enables using a reference of the abstract class to point to objects of its subclasses.
2. What is an Abstract Class?
An abstract class is declared using the abstract keyword.
Abstract members are declared without implementation, and concrete members can have full implementations.
abstract class Aircraft(val name: String) {
abstract fun fly() // Must be implemented by subclasses
open fun startEngine() { // Optional override
println("$name engine started")
}
}
3. Implementing Abstract Classes
Subclasses must implement all abstract members:
class Jet(name: String) : Aircraft(name) {
override fun fly() {
println("$name is flying at supersonic speed")
}
override fun startEngine() {
super.startEngine() // Optionally call parent implementation
println("Jet-specific engine checks")
}
}
fun main() {
val jet = Jet("F-16")
jet.startEngine()
jet.fly()
}
Output:
F-16 engine started
Jet-specific engine checks
F-16 is flying at supersonic speed
4. Key Points About Abstract Classes
- Cannot be instantiated directly:
val a = Aircraft() is not allowed.
- Can have both abstract and concrete members.
- Subclasses must override abstract members but may optionally override open members.
- Useful when you want to provide some default behavior while enforcing implementation of certain methods.
5. Abstract vs Interface
- Abstract class: Can have constructors, state (properties), and both abstract and concrete methods.
- Interface: Cannot have state (prior to Kotlin 1.1; now interfaces can have properties with default getters), usually used for pure behavior contracts.
- Use abstract classes when subclasses share common state or behavior; use interfaces for multiple behavior contracts.
6. Technical Interview Questions
- What is an abstract class in Kotlin?
A class that cannot be instantiated and can contain abstract (unimplemented) and concrete (implemented) members.
- Can abstract classes have constructors?
Yes, abstract classes can have primary or secondary constructors which can be called by subclasses.
- Can an abstract class contain properties?
Yes, it can have properties with or without initial values.
- Difference between abstract class and interface?
Abstract class can have state and constructors, while interface is typically used for behavior contracts and multiple implementations.
- Can a subclass avoid overriding an abstract member?
No, all abstract members must be implemented in the concrete subclass.
7. Key Takeaways
- Abstract classes are used to define a common blueprint with partial implementation.
- They enforce contracts for subclasses while allowing shared behavior.
- Abstract members must be overridden; open members are optional.
- Use abstract classes when subclasses share common state or functionality.