Skip to content
DeveloperMemos

Using Abstract Classes in Kotlin

Kotlin, Abstract Classes2 min read

When developing applications, it is often necessary to create classes that serve as a blueprint for other related classes. Kotlin provides a powerful feature called abstract classes that allows us to define common properties and methods while leaving specific implementation details to the subclasses. In this article, we will delve into abstract classes and understand how they can be utilized effectively in Kotlin development.

Understanding Abstract Classes

An abstract class in Kotlin is a class marked with the abstract keyword. It cannot be instantiated directly but serves as a base for other classes to inherit from. Abstract classes can contain both regular and abstract methods, where regular methods have an implementation, and abstract methods do not.

To define an abstract class in Kotlin, we use the following syntax:

1abstract class Animal {
2 abstract fun makeSound()
3
4 fun eat() {
5 println("The animal is eating")
6 }
7}

In the example above, we have an abstract class Animal. It declares an abstract method makeSound() that does not have an implementation. Additionally, it defines a regular method eat() that provides a default implementation.

Inheriting from Abstract Classes

To create a subclass from an abstract class, we use the :, followed by the name of the abstract class, after the subclass declaration. The subclass must provide implementations for all the abstract methods defined in the abstract class.

Let's create a concrete class Dog that inherits from the Animal abstract class:

1class Dog : Animal() {
2 override fun makeSound() {
3 println("The dog barks")
4 }
5}

In the example above, the Dog class extends the Animal abstract class and overrides the abstract method makeSound(). It provides its own implementation by printing "The dog barks" to the console.

Some Practical Use Cases

Abstract classes find application in various scenarios, including but not limited to:

  • Framework Development: Abstract classes are commonly used in frameworks to provide a foundation for other classes to build upon. They define the core functionality while leaving specific implementation details to be filled in by subclasses.
  • Modeling Real-World Entities: In domains where entities share common characteristics, abstract classes can be employed to define shared attributes and behaviors. For example, in an application dealing with different types of vehicles, an abstract class like Vehicle could encompass properties such as fuelType and methods like startEngine(), which can be further specialized by concrete vehicle classes.
  • Interface Implementation: Abstract classes can act as intermediaries between interfaces and their implementing classes. They can provide default implementations for some methods defined by the interface, allowing subclasses to focus on implementing only the necessary methods.

In Summary

Abstract classes are a powerful construct in Kotlin that allows us to define common behavior and provide a foundation for subclasses. By utilizing abstract classes effectively, we can promote code reusability, achieve polymorphism, and enforce contractual obligations. Whether in framework development or modeling real-world entities, abstract classes play a crucial role in building robust and extensible applications.