Kotlin Class: Shape, circle, and rectangle classes with area calculation
Write a Kotlin program that creates a class 'Shape' with an abstract function to calculate the area. Create subclasses 'Circle' and 'Rectangle' that override the area calculation function.
Pre-Knowledge (Before You Start!)
Before attempting this exercise, you should be familiar with the following concepts:
- Abstract Classes: In Kotlin, an abstract class cannot be instantiated and is used as a base class for other classes.
- Inheritance: A subclass can inherit properties and functions from a parent class and override them.
- Polymorphism: The ability to call overridden functions from a base class reference.
- Math Functions: Using Math.PI for calculating the area of a circle.
- Function Overriding: Overriding the abstract function calculateArea() in subclasses to provide specific implementations.
Hints (Try Before Looking at the Solution!)
Try to solve the problem using these hints:
- Hint 1: Create an abstract class Shape with an abstract function calculateArea() that returns a Double.
- Hint 2: Define two subclasses, Circle and Rectangle, that extend the Shape class.
- Hint 3: Implement the calculateArea() function in the Circle class using the formula π * radius².
- Hint 4: Implement the calculateArea() function in the Rectangle class using the formula width * height.
- Hint 5: In the main() function, create instances of Circle and Rectangle, call their respective calculateArea() functions, and print the results.
Sample Solution:
Kotlin Code:
abstract class Shape {
abstract fun calculateArea(): Double
}
class Circle(private val radius: Double) : Shape() {
override fun calculateArea(): Double {
return Math.PI * radius * radius
}
}
class Rectangle(private val width: Double, private val height: Double) : Shape() {
override fun calculateArea(): Double {
return width * height
}
}
fun main() {
val circle = Circle(7.0)
val circleArea = circle.calculateArea()
println("Circle Area: $circleArea")
val rectangle = Rectangle(5.0, 7.0)
val rectangleArea = rectangle.calculateArea()
println("Rectangle Area: $rectangleArea")
}
Sample Output:
Circle Area: 153.93804002589985 Rectangle Area: 35.0
Explanation:
In the above exercise -
- First we define an abstract class "Shape" with an abstract function "calculateArea()". This function does not have an implementation in the "Shape" class and must be overridden in its subclasses.
- We create two subclasses: Circle and Rectangle. The Circle class takes the radius as a constructor parameter and overrides the "calculateArea()" function to calculate the area of the circle using the formula π * radius^2.
- Similarly, the "Rectangle" class takes width and height as constructor parameters and overrides the "calculateArea()" function to calculate the area of the rectangle using the formula width * height.
- In the main function, we create instances of Circle and Rectangle and call the "calculateArea()" function on each of them to calculate their respective areas. The calculated areas are then printed.
Kotlin Editor:
Previous: MathUtils class with static functions.
Next: Person class with nested ContactInfo class.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics