Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Implementing interfaces
📖 Scenario: You are creating a simple program to manage different types of vehicles. Each vehicle can start and stop, but the way they do it might differ. To keep things organized, you will use an interface to define these actions.
🎯 Goal: Build a C# program that defines an interface IVehicle with methods Start() and Stop(). Then create two classes Car and Bike that implement this interface. Finally, create objects of these classes and call their methods.
📋 What You'll Learn
Create an interface called IVehicle with methods Start() and Stop().
Create a class called Car that implements IVehicle.
Create a class called Bike that implements IVehicle.
In each class, implement Start() and Stop() methods with simple Console.WriteLine messages.
Create objects of Car and Bike and call their Start() and Stop() methods.
💡 Why This Matters
🌍 Real World
Interfaces are used in software to define common behaviors that different classes must implement, like how different vehicles share start and stop actions.
💼 Career
Understanding interfaces is essential for writing clean, maintainable code in many programming jobs, especially in object-oriented programming.
Progress0 / 4 steps
1
Create the interface IVehicle
Create an interface called IVehicle with two methods: void Start() and void Stop().
C Sharp (C#)
Hint
An interface in C# uses the interface keyword and only declares method signatures without bodies.
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" using Console.WriteLine.
C Sharp (C#)
Hint
Use public class Car : IVehicle to implement the interface. Implement both methods with Console.WriteLine messages.
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" using Console.WriteLine.
C Sharp (C#)
Hint
Similar to Car, implement Bike with the interface methods printing the correct messages.
4
Create objects and call methods
In the Main method, create an object of Car called myCar and an object of Bike called myBike. Call Start() and Stop() on both objects. Use Console.WriteLine to display the messages from these methods.
C Sharp (C#)
Hint
Create objects using new keyword and call the methods directly. The output should show the messages in order.
Practice
(1/5)
1. What does it mean to implement an interface in C#?
easy
A. A class provides code for all methods declared in the interface.
B. An interface inherits from a class.
C. A class hides all methods of the interface.
D. An interface creates objects directly.
Solution
Step 1: Understand interface purpose
An interface declares methods without code, setting a contract.
Step 2: Implementing means coding methods
A class that implements the interface must write the code for all those methods.
Final Answer:
A class provides code for all methods declared in the interface. -> Option A
Hint: Implementing means writing all interface methods in the class [OK]
Common Mistakes:
Thinking interfaces can create objects
Believing interfaces inherit from classes
Assuming methods are hidden, not implemented
2. Which of the following is the correct syntax to implement an interface IMyInterface in a class MyClass?
easy
A. interface MyClass : IMyInterface { }
B. class MyClass : IMyInterface { }
C. class MyClass implements IMyInterface { }
D. class MyClass inherits IMyInterface { }
Solution
Step 1: Recall C# interface syntax
In C#, a class implements an interface using a colon (:), not 'implements' or 'inherits'.
Step 2: Check each option
class MyClass : IMyInterface { } uses correct syntax: class MyClass : IMyInterface { }. Others use wrong keywords or declare interface as class.
Final Answer:
class MyClass : IMyInterface { } -> Option B
Quick Check:
Use colon (:) to implement interface [OK]
Hint: Use ':' to implement interface, not 'implements' or 'inherits' [OK]
Common Mistakes:
Using 'implements' keyword like Java
Trying to inherit interface with 'inherits'
Declaring interface as a class
3. What will be the output of this code?
interface IGreet { void SayHello(); } class Person : IGreet { public void SayHello() { Console.WriteLine("Hi!"); } } var p = new Person(); p.SayHello();
medium
A. No output
B. SayHello
C. Hi!
D. Compile error
Solution
Step 1: Understand interface and class
The interface IGreet requires SayHello method. Person implements it by printing "Hi!".
Step 2: Trace code execution
Creating Person object and calling SayHello prints "Hi!" to console.
Final Answer:
Hi! -> Option C
Quick Check:
Implemented method runs and prints output [OK]
Hint: Implemented method runs exactly as coded in class [OK]