Introduction
An abstract class is a class that cannot be instantiated directly; it serves as a blueprint for other classes. Abstract classes can contain both abstract members, which have no implementation and must be overridden, and concrete members, which already provide behavior. This makes them well suited for sharing common state and logic across a family of related subclasses while still forcing each subclass to implement the behavior specific to it. The abstract keyword marks both the class and any members that lack an implementation. Subclasses must override every abstract member using the override keyword, or they must also be declared abstract themselves. Unlike interfaces, abstract classes can hold constructor parameters and mutable state, and a class can extend only one abstract class (Kotlin has single inheritance for classes), while it can implement multiple interfaces.
Cricket analogy: An abstract class is like the BCCI's fitness protocol template: it mandates every player run the same yo-yo test (abstract member) but lets each franchise decide net-practice drills (concrete member) on its own.
Syntax
abstract class Shape {
abstract fun area(): Double
fun describe(): String = "Area is ${area()}"
}Example
class Circle(val radius: Double) : Shape() {
override fun area(): Double = Math.PI * radius * radius
}
class Rectangle(val width: Double, val height: Double) : Shape() {
override fun area(): Double = width * height
}
fun main() {
val shapes = listOf(Circle(2.0), Rectangle(3.0, 4.0))
for (shape in shapes) {
println(shape.describe())
}
}Output
Area is 12.566370614359172
Area is 12.0Key Takeaways
- Abstract classes cannot be instantiated directly.
- They can mix abstract members (no body) and concrete members (with body).
- Subclasses must override all abstract members unless they too are abstract.
- Abstract classes can hold constructor parameters and state, unlike interfaces.
- A class can extend only one abstract class but implement many interfaces.
Practice what you learned
1. Can an abstract class be instantiated directly in Kotlin?
2. What must a non-abstract subclass do with the abstract members it inherits?
3. How many abstract classes can a single Kotlin class extend?
4. What is a key difference between abstract classes and interfaces in Kotlin?
5. Can an abstract class contain fully implemented (concrete) functions?
Was this page helpful?
You May Also Like
Interfaces in Kotlin
Explore how Kotlin interfaces define abstract methods, default implementations, and properties that classes can implement.
Inheritance in Kotlin
Learn how open classes, open members, and the override keyword enable single-class inheritance in Kotlin.
Classes in Kotlin
Learn how Kotlin classes bundle state and behavior with concise constructor syntax and final-by-default semantics.
Sealed Classes in Kotlin
Sealed classes restrict a class hierarchy to a known, closed set of subclasses, enabling exhaustive when checks.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics