You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Instructions/Labs/l2p2-lp2-m1-exercise-implement-interfaces-project.md
+24-49Lines changed: 24 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,12 +45,6 @@ This exercise includes the following tasks:
45
45
46
46
To start, you need to create a new C# project in your development environment. This project will serve as the foundation for implementing interfaces and their respective classes.
47
47
48
-
### What you'll learn
49
-
50
-
- How to create a new C# console application using the .NET CLI.
51
-
52
-
### Steps
53
-
54
48
1. Open Visual Studio Code.
55
49
1. Ensure that the C# Dev Kit extension is installed.
56
50
1. Open the terminal in Visual Studio Code by selecting `View > Terminal`.
@@ -79,13 +73,11 @@ Ensure that the project has been created successfully by verifying the presence
79
73
80
74
---
81
75
82
-
## Task 2: Define an interface with method signatures and properties
76
+
## Task 2: Define the `IPerson` interface with method signatures and properties
83
77
84
78
Next, you will define an interface that includes method signatures and properties. This interface will be used to enforce a contract for any class that implements it. The code defines an interface in C# which shows interfaces enforcing consistent behavior across classes.
85
79
86
-
### Steps
87
-
88
-
1. In the `ImplementInterfaces` project, create a new file named `IPerson.cs`.
80
+
1. Create a new file named `IPerson.cs` in the `ImplementInterfaces` project.
89
81
1. Add the following code to define the `IPerson` interface:
90
82
91
83
```csharp
@@ -100,18 +92,14 @@ Next, you will define an interface that includes method signatures and propertie
100
92
}
101
93
```
102
94
103
-
### Check your work
104
-
105
-
Verify that the `IPerson` interface is correctly defined by checking the `IPerson.cs` file. The interface should include the `Name` and `Age` properties, as well as the `DisplayInfo` method signature.
95
+
*The `IPerson` interface has been defined to include the `Name` and `Age` properties, along with the `DisplayInfo` method signature, ensuring a consistent contract for implementing classes.*
106
96
107
97
---
108
98
109
-
## Task 3: Implement the defined interface in a class
99
+
## Task 3: Implement the defined `IPerson`interface in a class
110
100
111
101
Now, you will create a class that implements the `IPerson` interface. This class will provide concrete implementations for the interface members. The code in this step implements an interface in a class and provides concrete implementations for the interface.
112
102
113
-
### Steps
114
-
115
103
1. In the `ImplementInterfaces` project, create a new file named `Student.cs`.
116
104
1. Add the following code to implement the `IPerson` interface in the `Student` class:
117
105
@@ -131,18 +119,15 @@ Now, you will create a class that implements the `IPerson` interface. This class
131
119
}
132
120
```
133
121
134
-
### Check your work
135
-
Ensure that the `Student` class correctly implements the `IPerson` interface by checking the `Student.cs` file. The class should provide implementations for the `Name` and `Age` properties, as well as the `DisplayInfo` method.
122
+
*The `Student` class has been implemented to adhere to the `IPerson` interface. It provides concrete implementations for the `Name` and `Age` properties, as well as the `DisplayInfo` method, ensuring compliance with the interface contract.*
136
123
137
124
---
138
125
139
-
## Task 4: Create another class that implements different behavior
126
+
## Task 4: Create a second class to implement `IPerson` interface with different behavior
140
127
141
128
You will now create another class that implements the `IPerson` interface but with different behavior. Task 4 implements the same interface in multiple classes. This demonstrates how to provide unique behavior for each class using an interface.
142
129
143
-
### Steps
144
-
145
-
1. In the `ImplementInterfaces` project, create a new file named `Teacher.cs`.
130
+
1. Create a new file named `Teacher.cs` in the `ImplementInterfaces` project.
146
131
1. Add the following code to implement the `IPerson` interface in the `Teacher` class:
147
132
148
133
```csharp
@@ -161,18 +146,14 @@ You will now create another class that implements the `IPerson` interface but wi
161
146
}
162
147
```
163
148
164
-
### Check your work
165
-
166
-
Verify that the `Teacher` class correctly implements the `IPerson` interface by checking the `Teacher.cs` file. The class should provide implementations for the `Name` and `Age` properties, as well as the `DisplayInfo` method.
149
+
*You have created the `Teacher` class, which implements the `IPerson` interface. This class provides its own implementation for the `Name` and `Age` properties, as well as the `DisplayInfo` method, ensuring it adheres to the interface contract.*
167
150
168
151
---
169
152
170
-
## Task 5: Demonstrate interface implementation
153
+
## Task 5: Demonstrate the interface implementation
171
154
172
155
In this task, you will demonstrate the use of the interface by creating instances of the `Student` and `Teacher` classes and calling their methods. This demonstrates how to use polymorphism to treat objects of different classes as the same interface type.
173
156
174
-
### Steps
175
-
176
157
1. Open the `Program.cs` file in the `ImplementInterfaces` project.
177
158
1. Replace the existing code with the following:
178
159
@@ -195,41 +176,35 @@ In this task, you will demonstrate the use of the interface by creating instance
195
176
}
196
177
```
197
178
198
-
### Check your work
199
-
200
-
Run the application using the following command:
201
-
202
-
```bash
203
-
dotnet run
204
-
```
205
-
206
-
You should see the output displaying the information for both the student and the teacher, demonstrating the interface implementation.
207
-
208
-
---
179
+
*The `Program.cs` file demonstrates how to use the `IPerson` interface to create polymorphic behavior. By creating instances of the `Student` and `Teacher` classes, you can observe how each class adheres to the interface contract while providing its own unique implementation of the `DisplayInfo` method.*
209
180
210
181
## Task 6: Test the implemented interfaces
211
182
212
183
Next, you will test the implemented interfaces and their respective classes to ensure they function correctly. This section of code demonstrates how to test interface implementations in a C# application.
213
184
214
-
### Steps
185
+
1. Ensure that the `Program.cs` file contains the code to create instances of `Student` and `Teacher` and calls their `DisplayInfo` methods and all files are saved.
186
+
1. Build the application using the following command:
187
+
188
+
```bash
189
+
dotnet build
190
+
```
191
+
192
+
*Ensure that the build completes successfully without any errors.*
215
193
216
-
1. Ensure that the `Program.cs` file contains the code to create instances of `Student` and `Teacher` and calls their `DisplayInfo` methods.
217
194
1. Run the application again using the following command:
218
195
219
196
```bash
220
197
dotnet run
221
198
```
222
199
223
-
1. Verify the output to ensure that the information for both the student and the teacher is displayed correctly.
224
-
225
-
### Check your work
200
+
1. Confirm that the application runs without errors and displays the correct information for both the student and the teacher. The output should look similar to the following:
226
201
227
-
Confirm that the application runs without errors and displays the correct information for both the student and the teacher. The output should look similar to the following:
202
+
```console
203
+
Student Name: Eric Solomon, Age: 20
204
+
Teacher Name: Kayla Lewis, Age: 35
205
+
```
228
206
229
-
```console
230
-
Student Name: Eric Solomon, Age: 20
231
-
Teacher Name: Kayla Lewis, Age: 35
232
-
```
207
+
*Observe that the output confirms the successful implementation of the `IPerson` interface in both the `Student` and `Teacher` classes. Each class adheres to the interface contract while providing its unique implementation of the `DisplayInfo` method, demonstrating polymorphism in action.*
0 commit comments