How to work with an abstract class in Kotlin
Abstract classes are classes that cannot be instantiated, which means that we cannot create objects of an abstract class. The main inspiration behind using abstract classes is that we can inherit from them. When a class inherits from an abstract class, it implements all abstract methods of the parent class.
Getting ready
You need to install a preferred development environment that compiles and runs Kotlin. You can also use the command line for the purpose, for which you need Kotlin compiler installed along with JDK. I am using online IDE at https://try.kotlinlang.org/ to compile and run my Kotlin code for this recipe.
How to do it...
Let's now see how to work with an abstract
class in these steps:
- The
abstract
keyword is used to declare anabstract
class. Let's create an abstract class and try to inherit from it:
abstract class Mammal { abstract fun move(direction: String) }
- For a class to be a subclass of the
Mammal
class, we use the:
operator...