Kotlin: Cannot create an instance of an abstract class
In this tutorial, we shall learn to fix Kotlin: Cannot create an instance of an abstract class. This compiler error occurs when Kotlin code tries to create an object directly from an abstract class. An abstract class is incomplete by design, so Kotlin allows it to be used only as a superclass, not as a directly instantiated class.
Following is a sample of the error you might get during compilation.
Error:(2, 27) Kotlin: Cannot create an instance of an abstract class
Why Kotlin cannot instantiate an abstract class
In Kotlin, we cannot create an instance of an abstract class.
An abstract class may contain abstract properties or abstract functions that do not have complete implementations. If Kotlin allowed direct object creation from such a class, the object could contain missing behavior. Therefore, the compiler stops the program before it can run.
The following syntax shows the kind of code that causes this error.
abstract class Shape {
abstract fun area(): Double
}
val shape = Shape() // Error: Cannot create an instance of an abstract class
Here, Shape has an abstract function named area(). Since the class does not define how the area is calculated, Kotlin does not allow Shape().
Fix Kotlin abstract class instantiation error by creating a subclass
Abstract class could only be inherited by a class or another Abstract class. So, to use abstract class, create another class that inherits the Abstract class.
The subclass must implement all abstract properties and functions. After that, create an object of the subclass instead of the abstract class.
Following is an example that demonstrates the usage of kotlin abstract class.
Kotlin Program – example.kt
/**
* Abstract Class
*/
abstract class Vehicle {
// regular variable
var name : String = "Not Specified"
// abstract variable
abstract var medium : String
// regular function
fun runsWhere() {
println("The vehicle, $name, runs on $medium")
}
// abstract function
abstract fun howItRuns()
}
/**
* inheriting abstract class
*/
class Aeroplane : Vehicle() {
// override abstract variables and functions of the
// abstract class that is inherited
override var medium: String = "air"
override fun howItRuns() {
println("Aeroplane fly based on buoyancy force.")
}
}
fun main(args: Array<String>) {
var vehicle1 = Aeroplane()
vehicle1.howItRuns()
}
Output
Aeroplane fly based on buoyancy force.
In the above program, Vehicle is abstract and cannot be instantiated. Aeroplane extends Vehicle, overrides the abstract property medium, and implements the abstract function howItRuns(). Therefore, Aeroplane() is valid.
Minimal Kotlin example that fixes Cannot create an instance of an abstract class
If you want a smaller example, compare the invalid abstract class instantiation with the corrected subclass instantiation below.
abstract class Animal {
abstract fun makeSound()
}
// val animal = Animal() // Not allowed
The correct approach is to create a concrete class that extends Animal.
abstract class Animal {
abstract fun makeSound()
}
class Dog : Animal() {
override fun makeSound() {
println("Dog barks")
}
}
fun main() {
val animal: Animal = Dog()
animal.makeSound()
}
Output:
Dog barks
Notice that the variable type can still be Animal. The object itself is Dog. This is a common and useful pattern in Kotlin: write code against the abstract type, but instantiate a concrete subclass.
Kotlin abstract class with constructor parameters
An abstract class in Kotlin can have a constructor. The constructor is not used to create an object of the abstract class directly. It is called when a concrete subclass object is created.
abstract class Employee(val name: String) {
abstract fun calculatePay(): Int
fun printName() {
println("Employee: $name")
}
}
class Developer(name: String, private val monthlyPay: Int) : Employee(name) {
override fun calculatePay(): Int {
return monthlyPay
}
}
fun main() {
val employee: Employee = Developer("Arun", 60000)
employee.printName()
println(employee.calculatePay())
}
Output:
Employee: Arun
60000
In this example, Employee has a constructor parameter name. The concrete class Developer passes a value to the superclass constructor by using : Employee(name).
Can an abstract class be instantiated using an anonymous object in Kotlin?
You still cannot instantiate an abstract class directly, but Kotlin can create an object expression that extends the abstract class and implements the missing members immediately. This is useful for short, local implementations.
abstract class ButtonClickHandler {
abstract fun onClick()
}
fun main() {
val handler = object : ButtonClickHandler() {
override fun onClick() {
println("Button clicked")
}
}
handler.onClick()
}
Output:
Button clicked
In this case, Kotlin is not creating an object of ButtonClickHandler alone. It creates an anonymous subclass of ButtonClickHandler, and that anonymous subclass provides the required implementation of onClick().
Abstract class, open class, and interface choices in Kotlin
The error often appears when the design should be adjusted. Use the following comparison to decide whether your type should be abstract, open, or an interface.
| Kotlin type | Can be instantiated directly? | Typical use |
|---|---|---|
abstract class | No | Use when subclasses must complete missing behavior and may share common state or functions. |
open class | Yes | Use when the base class is complete, but subclasses are allowed to extend or override behavior. |
interface | No | Use when you want to define a behavior contract that different classes can implement. |
If the base type is complete and you need to create objects from it, it should not be abstract. Use a normal class or an open class depending on whether you want inheritance.
open class Vehicle(val name: String)
val vehicle = Vehicle("Car") // Allowed because Vehicle is not abstract
Checklist to fix Cannot create an instance of an abstract class in Kotlin
- Check the class declaration and confirm whether it uses the
abstractkeyword. - Do not call the constructor of the abstract class directly, such as
Vehicle(). - Create a concrete subclass that extends the abstract class.
- Implement every abstract property and abstract function in the subclass using
override. - Create an object of the concrete subclass, or use an anonymous object expression for a small local implementation.
- If the base class should be directly instantiated, remove
abstractand use a normal oropenclass instead.
Kotlin Cannot create an instance of an abstract class FAQ
Is it possible to create an instance of an abstract class in Kotlin?
No. Kotlin does not allow direct instantiation of an abstract class. You must create an instance of a concrete subclass that extends the abstract class and implements all abstract members.
Why does Kotlin show Cannot create an instance of an abstract class?
Kotlin shows this error because an abstract class may contain incomplete members, such as abstract functions or properties. The compiler prevents object creation until a concrete subclass supplies those implementations.
Can an abstract class have a constructor in Kotlin?
Yes. An abstract class can have a constructor, but that constructor is called through a subclass constructor. It cannot be used to create an object of the abstract class directly.
Can I use an anonymous object for a Kotlin abstract class?
Yes. You can use object : AbstractClassName() and implement the required abstract members inside the object expression. This creates an anonymous subclass, not a direct instance of the abstract class.
Should I use an abstract class or interface to avoid this Kotlin error?
Use an abstract class when subclasses need shared state, constructors, or common functions. Use an interface when you only need a behavior contract that many unrelated classes can implement.
Editorial QA checklist for this Kotlin abstract class error tutorial
- Confirms that the tutorial explains why abstract classes cannot be instantiated directly in Kotlin.
- Includes a corrected subclass example that uses
overridefor abstract members. - Shows the difference between direct abstract class instantiation and concrete subclass instantiation.
- Covers constructor behavior for Kotlin abstract classes.
- Explains anonymous object expressions without implying that they instantiate the abstract class directly.
- Uses
language-kotlin syntaxonly for syntax demonstrations andoutputonly for result blocks.
Conclusion for Kotlin abstract class instantiation error
The error Cannot create an instance of an abstract class means the code is trying to instantiate an incomplete type. To fix it, create a concrete subclass, implement the abstract members, and instantiate that subclass. If the class is meant to be used directly, make it a normal class or an open class instead of an abstract class.
TutorialKart.com