0
0
Kotlinprogramming~30 mins

Abstract classes and methods in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Abstract classes and methods
📖 Scenario: You are creating a simple program to represent different types of vehicles. Each vehicle has a way to describe itself, but the exact description depends on the type of vehicle.
🎯 Goal: Build an abstract class called Vehicle with an abstract method describe(). Then create two classes Car and Bike that inherit from Vehicle and provide their own description. Finally, print the descriptions of both vehicles.
📋 What You'll Learn
Create an abstract class called Vehicle
Add an abstract method describe() that returns a String
Create a class Car that inherits from Vehicle and implements describe()
Create a class Bike that inherits from Vehicle and implements describe()
Create instances of Car and Bike
Print the result of calling describe() on both instances
💡 Why This Matters
🌍 Real World
Abstract classes help organize code when you have different types of things that share some common behavior but also have their own specific details.
💼 Career
Understanding abstract classes and methods is important for designing clean and reusable code in many software development jobs.
Progress0 / 4 steps
1
Create the abstract class
Create an abstract class called Vehicle with an abstract method describe() that returns a String.
Kotlin
Need a hint?

Use the abstract keyword before the class and the method. The method should have no body.

2
Create the Car class
Create a class called Car that inherits from Vehicle and implements the describe() method to return the string "This is a car.".
Kotlin
Need a hint?

Use class Car : Vehicle() to inherit and override fun describe() to provide the method body.

3
Create the Bike class
Create a class called Bike that inherits from Vehicle and implements the describe() method to return the string "This is a bike.".
Kotlin
Need a hint?

Similar to Car, inherit from Vehicle and override describe().

4
Create instances and print descriptions
Create an instance of Car called car and an instance of Bike called bike. Then print the result of calling describe() on both instances.
Kotlin
Need a hint?

Create the instances with val car = Car() and val bike = Bike(). Use println() to print their descriptions.