w3resource

Kotlin OOP Program: Shape base class and Polymorphic subclasses


Write a Kotlin object-oriented program that creates a base class Shape and derives subclasses Circle, Rectangle, and Triangle. Implement appropriate methods in each class and demonstrate polymorphism.


Pre-Knowledge (Before You Start!)

Before attempting this exercise, you should be familiar with the following concepts:

  • Abstract Classes: Understanding how to define an abstract class in Kotlin and declare abstract methods.
  • Inheritance: Using the : symbol to derive subclasses from a base class.
  • Polymorphism: How different subclasses can provide different implementations of the same method.
  • Mathematical Calculations: Using formulas for area and perimeter calculations of different shapes.

Hints (Try Before Looking at the Solution!)

Try to solve the problem using these hints:

  • Hint 1: Define an abstract class Shape with two abstract methods: area() and perimeter().
  • Hint 2: Create subclasses Circle, Rectangle, and Triangle that inherit from Shape.
  • Hint 3: Override the area() and perimeter() methods in each subclass and implement the formulas.
  • Hint 4: In the main() function, create instances of each shape and store them in a list.
  • Hint 5: Use a loop to iterate through the list and call the area() and perimeter() methods for each shape.

Sample Solution:

Kotlin Code:

abstract class Shape {
    abstract fun area(): Double
    abstract fun perimeter(): Double
}

class Circle(private val radius: Double) : Shape() {
    override fun area(): Double {
        return Math.PI * radius * radius
    }

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

class Rectangle(private val width: Double, private val height: Double) : Shape() {
    override fun area(): Double {
        return width * height
    }

    override fun perimeter(): Double {
        return 2 * (width + height)
    }
}

class Triangle(private val side1: Double, private val side2: Double, private val side3: Double) : Shape() {
    override fun area(): Double {
        val semiPerimeter = perimeter() / 2
        return Math.sqrt(semiPerimeter * (semiPerimeter - side1) * (semiPerimeter - side2) * (semiPerimeter - side3))
    }

    override fun perimeter(): Double {
        return side1 + side2 + side3
    }
}

fun main() {
    val circle = Circle(4.5)
    val rectangle = Rectangle(7.0, 11.0)
    val triangle = Triangle(4.0, 5.0, 6.0)

    val shapes = listOf(circle, rectangle, triangle)

    for (shape in shapes) {
        println("Area: ${shape.area()}")
        println("Perimeter: ${shape.perimeter()}")
        println()
    }
}

Sample Output:

Area: 63.61725123519331
Perimeter: 28.274333882308138

Area: 77.0
Perimeter: 36.0

Area: 9.921567416492215
Perimeter: 15.0

Explanation:

In the above exercise -

  • The "Shape" class is declared as an abstract class with two abstract methods: "area()" and "perimeter()". The Circle, Rectangle, and Triangle classes inherit from the "Shape" class and provide their own implementations of these methods.
  • In the "main()" function, instances of Circle, Rectangle, and Triangle are created and stored in a list. Using a loop, the program demonstrates polymorphism by calling the area() and perimeter() methods on each shape object. This dynamically invokes the appropriate implementation based on the actual object type.
  • The output of the program will display the area and perimeter of each shape.

Go to:


PREV : Kotlin OOP Exercises Home.
NEXT : Implementing a logger for logging functionality.

Kotlin Editor:


Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.