🧱 Kotlin – Abstract Classes (2025 Guide)
Learn how abstract classes work in Kotlin, when to use them, and how they differ from interfaces—with syntax, examples, and FAQs.
🚀 What is an Abstract Class in Kotlin?
An abstract class in Kotlin is a class that cannot be instantiated directly. It acts as a base class that defines common behavior for subclasses and can include both abstract methods (without body) and concrete methods (with body).
📌 Use abstract classes when:
- You want to provide a partial implementation.
- You want to enforce a common base for related classes.
🧩 Key Features
- Can contain abstract and non-abstract members.
- Cannot be instantiated.
- Subclasses must override abstract members.
🧱 Kotlin Abstract Class Syntax
abstract class Animal {
abstract fun makeSound()
fun breathe() {
println("Breathing...")
}
}
🔍 In the above code:
makeSound()is an abstract function – must be overridden.breathe()is a concrete method – optional to override.
🐾 Example – Abstract Class Implementation
abstract class Animal {
abstract fun makeSound()
fun eat() {
println("Eating...")
}
}
class Dog : Animal() {
override fun makeSound() {
println("Woof!")
}
}
fun main() {
val dog = Dog()
dog.makeSound() // Output: Woof!
dog.eat() // Output: Eating...
}
✅ Dog class implements the abstract function and inherits the concrete function.
🆚 Abstract Class vs Interface in Kotlin
| Feature | Abstract Class | Interface |
|---|---|---|
| Can contain state (fields) | ✅ Yes | ⚠️ From Kotlin 1.1+ only |
| Multiple inheritance | ❌ No | ✅ Yes |
| Constructor | ✅ Can have | ❌ Cannot have |
| Default implementation | ✅ Yes | ✅ Yes |
🧠 Use abstract class when:
- You need to share code/state among subclasses.
- You want to create a base class with constructor or properties.
🔄 Abstract Properties
abstract class Shape {
abstract val name: String
}
class Circle : Shape() {
override val name = "Circle"
}
📌 Abstract properties must be overridden by the subclass.
❗ Restrictions
- Cannot be instantiated directly:
val animal = Animal() // ❌ Error - All abstract members must be implemented by concrete subclasses.
📋 Summary
| Concept | Description |
|---|---|
abstract class | Base class with or without method bodies |
abstract function | Must be overridden in subclasses |
abstract property | Must be initialized in subclasses |
open function | Optional to override |
❓ FAQ – Kotlin Abstract Classes
🔹 Q1: Can I create an object of an abstract class?
A: No. Abstract classes cannot be instantiated directly.
🔹 Q2: Can abstract classes have constructors?
A: Yes. You can define constructors just like in regular classes.
abstract class Base(val id: Int)
🔹 Q3: Can a Kotlin class extend both an abstract class and implement interfaces?
A: Yes. Kotlin supports single inheritance (one abstract class) and multiple interfaces.
class MyClass : AbstractClass(), MyInterface
🔹 Q4: Is an abstract class implicitly open?
A: Yes. You don’t need to use the open modifier with abstract classes.
🔹 Q5: Can I have a non-abstract class with only abstract methods?
A: No. If a class has even one abstract method or property, it must be declared abstract.
Share Now :
