What is the difference between an interface and a ...
# getting-started
e
What is the difference between an interface and a data class?
Broadly speaking, an interface defines functions and properties that a class can implement, while a data class is a class that has some additional behaviour that makes it suitable for holding data
A data class can implement an interface.
e
Ok, so an interface is just an abstract base class?
But with authority to force you to implement every function
s
Basically, yes. In Kotlin/Java, the main difference between an abstract class and an interface is that you can only extend from one class, but you can implement multiple interfaces.
j
interfaces can’t have state, classes can have
👍 1
abstract class can have state
j
These are not Kotlin-specific questions, but general OOP questions. Interfaces are abstract, but they are not classes so they can't hold state. That's the main difference between abstract classes and interfaces. Also, in Java and Kotlin, classes can implement multiple interfaces but only extend one abstract class. Now data classes are just a special case of regular classes, with extra abilities/convenience, like auto generated methods.
866 Views