Abstract Classes and Interfaces in Kotlin: A Complete Guide
Sagar Malhotra
Android dev by day, Creator by night.???? Sharing my passion through videos, blogs and conferences.??
Kotlin provides two mechanisms for defining a blueprint for classes: abstract classes and interfaces. Both abstract classes and interfaces play important roles in object-oriented programming, but they have different use cases and features.
Though in Kotlin, the difference between them is more blurry because:
So, you may think that the abstraction provided by abstract classes and Interfaces are similar. But actually, it's not.
Abstract Classes:
Definition: An abstract class is a class that cannot be instantiated on its own, but can be extended by other classes. Abstract classes can contain both abstract and non-abstract methods.
Properties:
Use Cases:
Example:
abstract class Shape {
abstract val name: String
abstract fun area(): Double
}
class Rectangle(width: Double, height: Double) : Shape() {
override val name: String = "Rectangle"
private val w: Double = width
private val h: Double = height
override fun area(): Double = w * h
}
class Circle(radius: Double) : Shape() {
override val name: String = "Circle"
private val r: Double = radius
override fun area(): Double = 3.14 * r * r
}
Interfaces:
Definition: An interface is a contract for a class that defines a set of abstract methods and properties. Interfaces can also contain default implementations for methods.
Properties:
Use Cases:
Example:
interface Animal {
val name: String
fun makeSound()
fun sleep() {
println("${name} is sleeping")
}
}
class Dog : Animal {
override val name: String = "Dog"
override fun makeSound() {
println("${name} barks")
}
}
class Cat : Animal {
override val name: String = "Cat"
override fun makeSound() {
println("${name} meows")
}
}
What is the Difference?
Hope, this will clear most of your doubts.
Why Method bodies:
In Abstract classes: Show the general behavior of the function. Like, what is the task of the function that we will expect if we are not gonna override the specific function.
Example: We can write a print() function in our Shape abstract class and the body will just say print("Shape is printing.."). So, in the case when we are not overriding this function, then still we can use this function and print a general thing, that we can use for all our subclasses.
In Interfaces: To provide the default output, so that our code will compile when we forgot to implement the function.
If this thing is still confusing just remember the purpose of why we are here using the Interface.
This is a very famous Interview question also, so if I was asked to define their difference in one line, then I can say:
Also, give clear examples and use cases.
You can also be trapped with twisted questions like, Interfaces have method bodies, private methods, then how they distinguish! See, all these are subtle differences but the main choosing factor is YOUR use case. Either you are defining a Type or a Standard behavior.
I hope you found this helpful. If yes, then do FOLLOW me for more Android-related content.