Kotlin Abstract Classes
In Kotlin, like in other object-oriented programming languages, an abstract class is a class that cannot be instantiated (meaning, you cannot create objects of an abstract class). Instead, it is designed to be subclassed by other classes.
Abstract classes are typically used to provide a base level of functionality that other classes can build upon.
Before we dive into the details, let's understand what the term "abstract" means in the context of programming. The term "abstract" is used for entities which are incomplete and can only be used if they are made complete.
Declaring an Abstract Class
In Kotlin, you declare an abstract class using the abstract
keyword. Here is an example:
abstract class Animal {
abstract fun sound()
}
In the above code, Animal
is an abstract class with an abstract function sound()
. Note that abstract functions in an abstract class are always open for overriding and they do not have a body. They are declared and defined in the class that inherits the abstract class.
Instantiate an Abstract Class
Remember, an abstract class cannot be instantiated. That means you cannot create an object of an abstract class. If you try to do so, the compiler will throw an error.
val animal = Animal() // Error: Cannot create an instance of an abstract class
Abstract Class Inheritance
An abstract class is open for inheritance by default. So, you do not need to use the open
keyword with the abstract class or its members. The subclasses of an abstract class that are not abstract must provide a definition for the abstract members of the superclass.
Here is an example:
abstract class Animal {
abstract fun sound()
}
class Dog : Animal() {
override fun sound() {
println("The dog barks")
}
}
fun main(args: Array<String>) {
val myDog: Animal = Dog()
myDog.sound() // prints "The dog barks"
}
In the above example, the abstract class Animal
has an abstract function sound()
. The Dog
class inherits the Animal
class and provides a definition for the sound()
function.
Non-Abstract Members
An abstract class can contain non-abstract members (properties or functions) as well. The non-abstract members of an abstract class are open by default, but you can restrict them from being overridden in the subclass by using the final
keyword.
Here is an example:
abstract class Animal {
abstract fun sound()
fun eat() {
println("The animal eats")
}
}
class Dog : Animal() {
override fun sound() {
println("The dog barks")
}
}
fun main(args: Array<String>) {
val myDog: Animal = Dog()
myDog.sound() // prints "The dog barks"
myDog.eat() // prints "The animal eats"
}
In the code above, the Animal
class has a non-abstract function eat()
. The Dog
class can use the eat()
function directly because it is inherited from the Animal
class.
Conclusion
In this guide, we have learned about abstract classes in Kotlin, how to declare them, why we cannot instantiate them, how they support inheritance, and how they can contain non-abstract members. Remember, abstract classes are crucial building blocks for creating large, scalable systems. They allow you to define common behavior and interfaces at a high level, and then fill in the details with concrete classes as needed.