What is an abstract class in Kotlin, and how do you declare an abstract member that subclasses must implement?
answer
- abstract = cannot instantiate
- abstract member = no body, must override
- concrete subclass overrides all or stays abstract
- can hold state + constructors unlike interfaces
- object : Abstract() {} fills it in
basics
~10 sAn abstract class is one you cannot create objects from directly. It can have members marked abstract that have no body, and any non-abstract subclass must provide their implementation.
solid answer
~30 sAn abstract class is declared with the abstract keyword and cannot be instantiated directly; you must subclass it. Inside it you can declare abstract members (functions or properties) using the abstract keyword with no body or initializer, e.g. abstract fun area(): Double. A non-abstract (concrete) subclass must override every inherited abstract member with the override keyword, or it must itself be declared abstract. Abstract classes can also contain normal concrete methods, state (backing fields), constructors, and init blocks. Trying to write val s = Shape() where Shape is abstract is a compile error.
code
kotlin · 11 linesabstract class Shape {
abstract fun area(): Double
fun printArea() = println(area())
}
class Square(val side: Double) : Shape() {
override fun area() = side * side
}
// val s = Shape() // ERROR: cannot create instance of abstract class
Square(2.0).printArea() // 4.0go deeper
Knows abstract classes can't be instantiated and abstract members must be overridden by concrete subclasses.
Explains mixing concrete and abstract members, constructors, and the exact compiler errors for missing overrides or instantiation.
Contrasts abstract classes vs interfaces (state, constructors, single inheritance) and knows anonymous-object instantiation.
Frames abstract classes as a design tool for shared invariant state and template behavior, weighing them against interfaces and composition.
## What 'abstract' means An **abstract class** is a class that is incomplete: it declares behavior but leaves some of it unimplemented, so it cannot be turned into an object by itself. You mark it with the `abstract` keyword. ```kotlin abstract class Shape { abstract val name: String // abstract property: no initializer abstract fun area(): Double // abstract function: no body fun describe(): String = // concrete method, fully implemented "$name has area $area" } ``` ## Abstract members A member marked `abstract` has **no implementation**: an abstract function has no `{ }` body, and an abstract property has no initializer and no getter body. It is a contract that says "every concrete subclass must supply this." ## Concrete subclasses must override A non-abstract subclass MUST provide a body for every inherited abstract member using `override`: ```kotlin class Circle(val r: Double) : Shape() { override val name = "circle" override fun area() = Math.PI * r * r } ``` If a subclass forgets one, the compiler reports "Class 'Circle' is not abstract and does not implement abstract member." A subclass may instead stay `abstract` and leave the member unimplemented for the next level down. ## Cannot instantiate `Shape()` is a compile error: "Cannot create an instance of an abstract class." You can only instantiate concrete subclasses, or use an `object : Shape() { ... }` anonymous object that fills in the abstract members. ## Mixing concrete and abstract Abstract classes are not all-or-nothing: they can hold normal methods, mutable/immutable state with backing fields, constructors, and `init` blocks — unlike interfaces, which have no constructor and no backing-field state.
- Can an abstract class have a constructor?Yes. It can declare primary and secondary constructors with parameters and init blocks; subclasses call them via the super-constructor call in their supertype list, e.g. `: Shape(x)`.
- Must every member of an abstract class be abstract?No. It can freely mix abstract members with fully implemented concrete methods, properties, and state.
An abstract class is like a blank form with some fields pre-filled and others left empty — you can't 'use' the blank form itself, only a completed copy.
saying these in an interview costs you the question
- Saying you can instantiate an abstract class directly
- Claiming all members of an abstract class must be abstract
- Forgetting the override keyword when implementing an abstract member
- Thinking an abstract class cannot hold state or a constructor
- Confusing abstract with private — abstract is about missing implementation, not visibility