0
0
Kotlinprogramming~30 mins

Interface declaration and implementation in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Interface declaration and implementation
📖 Scenario: You are creating a simple program to represent different types of vehicles. Each vehicle can start and stop. You will use an interface to define these actions and then create classes that implement this interface.
🎯 Goal: Build a Kotlin program that declares an interface called Vehicle with two functions: start() and stop(). Then create two classes, Car and Bike, that implement the Vehicle interface. Finally, create objects of these classes and call their start() and stop() functions.
📋 What You'll Learn
Declare an interface called Vehicle with two functions: start() and stop()
Create a class called Car that implements the Vehicle interface
Create a class called Bike that implements the Vehicle interface
In both classes, provide simple print statements inside start() and stop() functions
Create objects of Car and Bike and call their start() and stop() functions
💡 Why This Matters
🌍 Real World
Interfaces are used in real-world apps to define common behaviors for different objects, like vehicles, users, or devices, so they can be handled in a uniform way.
💼 Career
Understanding interfaces is important for Kotlin developers to write clean, modular, and maintainable code, especially in Android app development and backend services.
Progress0 / 4 steps
1
Declare the Vehicle interface
Declare an interface called Vehicle with two functions: start() and stop(). Both functions should have no parameters and no return value.
Kotlin
Need a hint?

Use the interface keyword to declare an interface. Inside, declare two functions start() and stop() without bodies.

2
Create the Car class implementing Vehicle
Create a class called Car that implements the Vehicle interface. Override the start() function to print "Car started" and the stop() function to print "Car stopped".
Kotlin
Need a hint?

Use class Car : Vehicle to implement the interface. Use override keyword for the functions and add println statements inside.

3
Create the Bike class implementing Vehicle
Create a class called Bike that implements the Vehicle interface. Override the start() function to print "Bike started" and the stop() function to print "Bike stopped".
Kotlin
Need a hint?

Similar to Car, create Bike class implementing Vehicle and override the functions with print statements.

4
Create objects and call functions
Create an object called myCar of type Car and an object called myBike of type Bike. Call start() and stop() on both objects.
Kotlin
Need a hint?

Create objects using val myCar = Car() and val myBike = Bike(). Then call start() and stop() on each object. Put this code inside a main() function.