0
0
C Sharp (C#)programming~30 mins

Interface as contract mental model in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Interface as contract mental model
📖 Scenario: Imagine you are building a simple system for different types of vehicles. Each vehicle must be able to start and stop. We will use an interface to make sure every vehicle follows this rule.
🎯 Goal: You will create an interface called IVehicle that acts like a contract. Then you will create classes that follow this contract by implementing the interface. Finally, you will show how to use these classes through the interface.
📋 What You'll Learn
Create an interface called IVehicle with two methods: Start() and Stop().
Create a class called Car that implements the IVehicle interface.
Create a class called Bike that implements the IVehicle interface.
Create a variable of type IVehicle and assign it to instances of Car and Bike.
Call the Start() and Stop() methods on the IVehicle variable and print messages.
💡 Why This Matters
🌍 Real World
Interfaces are used in software to ensure different parts follow the same rules, like how different vehicles must be able to start and stop.
💼 Career
Understanding interfaces is important for writing clean, maintainable code and working with many programming frameworks and libraries.
Progress0 / 4 steps
1
Create the IVehicle interface
Create an interface called IVehicle with two methods: void Start() and void Stop().
C Sharp (C#)
Need a hint?

An interface is like a promise that classes will have certain methods.

2
Create the Car class implementing IVehicle
Create a class called Car that implements the IVehicle interface. Implement the Start() method to print "Car started" and the Stop() method to print "Car stopped".
C Sharp (C#)
Need a hint?

Use : IVehicle after the class name to implement the interface.

3
Create the Bike class implementing IVehicle
Create a class called Bike that implements the IVehicle interface. Implement the Start() method to print "Bike started" and the Stop() method to print "Bike stopped".
C Sharp (C#)
Need a hint?

Implement the interface methods just like in the Car class.

4
Use the IVehicle interface to control vehicles
Create a variable of type IVehicle called vehicle. Assign it to a new Car() instance. Call vehicle.Start() and vehicle.Stop(). Then assign vehicle to a new Bike() instance. Call vehicle.Start() and vehicle.Stop() again.
C Sharp (C#)
Need a hint?

Use the interface type IVehicle to hold different vehicle objects and call their methods.