History of the question:
Kotlin combines the best of Java and the functional legacy of the JVM. Regular classes are declared as standard structures, abstract classes allow for the creation of under-defined templates with default implementation, and interfaces support multiple inheritance of behavior without state.
Problem:
The correct choice between a class, an abstract class, and an interface determines the architecture of the application, code granularity, and its extensibility. Incorrect inheritance leads to difficulties in testing and future changes.
Solution:
In Kotlin:
Code example:
interface Drawable { fun draw() } abstract class Shape(var color: String) : Drawable { abstract fun calcArea(): Double override fun draw() = println("Shape drawn") } class Circle(color: String, val radius: Double) : Shape(color) { override fun calcArea() = Math.PI * radius * radius }
Key features:
Can interfaces contain properties with backing field?
No, they can only define the property signature but cannot store data — properties without backing field.
Can you inherit from multiple classes?
No, Kotlin supports only single class inheritance but multiple interface implementations.
Can a constructor be declared in an interface?
No, an interface does not support constructors because it does not store state — only a behavior contract.
In the application, all common functions were moved to an abstract class, even if there was no internal logic or state, while the need was just for a common contract.
Pros:
Cons:
Only the necessary contracts were moved to interfaces, abstract classes were limited to common properties and methods requiring implementation.
Pros:
Cons: