0
0
Kotlinprogramming~30 mins

Interface with default implementations in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Interface with Default Implementations in Kotlin
📖 Scenario: Imagine you are creating a simple app to manage different types of vehicles. Each vehicle can start and stop, but some vehicles have a default way to start and stop.
🎯 Goal: You will create an interface called Vehicle with default implementations for start() and stop() methods. Then, you will create a class Car that uses these defaults and a class Bike that overrides the default start() method.
📋 What You'll Learn
Create an interface called Vehicle with default methods start() and stop()
Create a class called Car that implements Vehicle and uses default methods
Create a class called Bike that implements Vehicle and overrides the start() method
Create instances of Car and Bike and call their start() and stop() methods
Print the output of these method calls
💡 Why This Matters
🌍 Real World
Interfaces with default implementations help you write flexible and reusable code. For example, vehicle management systems can share common behavior while allowing specific vehicles to customize actions.
💼 Career
Understanding interfaces with default methods is important for Kotlin developers working on Android apps or backend services where code reuse and clean design are essential.
Progress0 / 4 steps
1
Create the Vehicle interface with default methods
Create an interface called Vehicle with two methods: start() and stop(). Provide default implementations for both methods that print "Vehicle is starting" and "Vehicle is stopping" respectively.
Kotlin
Need a hint?

Use fun start() { println(...) } inside the interface to provide default behavior.

2
Create the Car class implementing Vehicle using default methods
Create a class called Car that implements the Vehicle interface. Do not override any methods so it uses the default start() and stop() implementations.
Kotlin
Need a hint?

Just write class Car : Vehicle and leave the body empty to use defaults.

3
Create the Bike class overriding the start method
Create a class called Bike that implements the Vehicle interface. Override the start() method to print "Bike is starting differently" but keep the default stop() method.
Kotlin
Need a hint?

Use override fun start() { println(...) } inside Bike.

4
Create instances and call start and stop methods
Create instances of Car and Bike named myCar and myBike. Call start() and stop() on both instances. Print the output of these calls.
Kotlin
Need a hint?

Create val myCar = Car() and val myBike = Bike(). Then call start() and stop() on both.