LAST UPDATED: AUGUST 6, 2020
Kotlin Abstract Class
In the last tutorial we discussed about inheritance. We will take one step ahead and discuss about abstract classes in Kotlin now.
Suppose we have a class called Shape
. It has a function area()
to calculate the area of the shape. Now we want that each child class must implement this area()
function to calculate their respective areas. Instead of providing body of area()
method in Shape
class, we can mark it as abstract
.
By marking it abstract we ensure the following:
-
Now, there is no need to provide body of function definition of area()
function in Shape
class.
-
The child class must override this area()
function and provide its own implementation, else the compiler will give error.
Then if classes like Square
, Rectangle
, Circle
etc. inherits Shape
class, these classes must override area()
function otherwise compiler will throw an error.
Kotlin Abstract class
If any function of a class is marked with abstract
keyword then the class should also be marked with abstract
keyword. Let us first see a basic example of an abstract class:
abstract class Shape{
abstract fun area()
}
class Square: Shape() {
override fun area() {
println("Area of square: side*side")
}
}
class Circle: Shape() {
override fun area() {
println("Area of circle: 3.14*r*r")
}
}
fun main() {
val square = Square()
val circle = Circle()
square.area()
circle.area()
}
Area of square: side*side
Area of circle: 3.14*r*r
In this example, we created an abstract class Shape
. The Square
class and Circle
class inherits the Shape class and overrides area()
method.
Let us discuss some important points regarding abstract class:
-
If a class has an abstract property or abstract function, then class must be marked as abstract.
-
An abstract class cannot be instantiated. It means we cannot create object of an abstract class.
-
There is no need to add open keyword in abstract classes as they are meant to be inherited and hence by default open.
-
An abstract can have both abstract and non-abstract (normally defined) functions and properties.
-
An abstract function does not have body.
-
In abstract class, by default all members are non-abstract. We need to add abstract
keyword to make them abstract.
Let us see an example illustrating above points:
abstract class Shape{
abstract var sides: Int
abstract fun area()
fun sayShape(){
println("It is a shape...")
}
}
class Square: Shape() {
override var sides: Int = 4
override fun area() {
println("Area of square: side*side")
}
}
class Circle: Shape() {
override var sides: Int = 0
override fun area() {
println("Area of circle: 3.14*r*r")
}
}
fun main() {
val square = Square()
val circle = Circle()
println("Side of square: ${square.sides}")
println("Side of Circle: ${circle.sides}")
square.area()
circle.area()
square.sayShape()
circle.sayShape()
}
Side of square: 4
Side of Circle: 0
Area of square: side*side
Area of circle: 3.14*r*r
It is a shape...
It is a shape...
Summary
In this tutorial we discussed about abstract classes. In the next tutorial we will discuss about interfaces and their implementations.