Understanding Abstraction in Kotlin

Welcome to Abstraction

Welcome back! Previously, you delved into polymorphism and learned how to create more flexible code structures using classes and inheritance. In this session, we will take a step further and explore a crucial aspect of Object-Oriented Programming: Abstraction.

Understanding Abstract Classes and Abstract Methods

Abstract classes and abstract methods are essential tools for achieving abstraction. They allow you to define a common interface for a group of derived classes, ensuring that specific methods are implemented. This approach helps you write more robust and scalable programs.

1. Defining an Abstract Class

Let's revisit some of the key concepts through the following code example:

Kotlin
// Define an abstract class Shape. Note that the abstractness is achieved by having at least one abstract method.
abstract class Shape(private val color: String) {

    // Abstract methods for calculating the area and perimeter
    abstract fun area(): Double
    abstract fun perimeter(): Double

    // Concrete method to get the color
    fun getColor(): String {
        return color
    }
}

In this snippet, we define an abstract class Shape with a property color and a concrete method getColor to retrieve the color. The class also contains two abstract methods: area and perimeter. An abstract class can have properties and fully defined methods, but it must contain at least one abstract method, making it impossible to instantiate directly.

2. Implementing the Abstract Methods in Derived Classes

Next, we create concrete classes that extend the abstract class Shape.

Circle Class

Kotlin
// Define a Circle class that inherits from Shape
class Circle(private val radius: Double, color: String) : Shape(color) {

    // Implement the area and perimeter methods
    override fun area(): Double {
        return Math.PI * radius * radius
    }

    override fun perimeter(): Double {
        return 2 * Math.PI * radius
    }
}

Here, the Circle class inherits from Shape and provides concrete implementations for the abstract methods area and perimeter. It also includes a primary constructor to initialize the radius and color.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal