Skip to content

Commit f935c7b

Browse files
authored
Merge pull request #49405 from wwlpublish/f03d74d794def66d41cdc5e2822a9bd18a606243c37e8566345a59a751be2ccf-live
Modules/M02-implement-polymorphic-behavior
2 parents be3ab2f + da3e5fd commit f935c7b

17 files changed

+876
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.implement-polymorphic-behavior.introduction
3+
title: Introduction
4+
metadata:
5+
title: Introduction
6+
description: "Introduction"
7+
ms.date: 03/06/2025
8+
author: wwlpublish
9+
ms.author: cahowd
10+
ms.topic: unit
11+
durationInMinutes: 3
12+
content: |
13+
[!include[](includes/1-introduction.md)]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.implement-polymorphic-behavior.examine-principles-polymorphism
3+
title: Examine the principles of polymorphic behavior
4+
metadata:
5+
title: Examine the principles of polymorphic behavior
6+
description: "Examine the principles of polymorphic behavior"
7+
ms.date: 03/06/2025
8+
author: wwlpublish
9+
ms.author: cahowd
10+
ms.topic: unit
11+
durationInMinutes: 5
12+
content: |
13+
[!include[](includes/2-examine-principles-polymorphism.md)]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.implement-polymorphic-behavior.explore-inheritance-based-polymorphism
3+
title: Explore inheritance-based polymorphism
4+
metadata:
5+
title: Explore inheritance-based polymorphism
6+
description: "Explore inheritance-based polymorphism"
7+
ms.date: 03/06/2025
8+
author: wwlpublish
9+
ms.author: cahowd
10+
ms.topic: unit
11+
durationInMinutes: 10
12+
content: |
13+
[!include[](includes/3-explore-inheritance-based-polymorphism.md)]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.implement-polymorphic-behavior.explore-interface-based-polymorphism
3+
title: Explore interface-based polymorphism
4+
metadata:
5+
title: Explore interface-based polymorphism
6+
description: "Explore interface-based polymorphism"
7+
ms.date: 03/06/2025
8+
author: wwlpublish
9+
ms.author: cahowd
10+
ms.topic: unit
11+
durationInMinutes: 10
12+
content: |
13+
[!include[](includes/4-explore-interface-based-polymorphism.md)]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.implement-polymorphic-behavior.review-polymorphic-design-considerations
3+
title: Review polymorphic design considerations
4+
metadata:
5+
title: Review polymorphic design considerations
6+
description: "Review polymorphic design considerations"
7+
ms.date: 03/06/2025
8+
author: wwlpublish
9+
ms.author: cahowd
10+
ms.topic: unit
11+
durationInMinutes: 8
12+
content: |
13+
[!include[](includes/5-review-polymorphic-design-considerations.md)]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.implement-polymorphic-behavior.exercise-implement-polymorphism-csharp-app
3+
title: Exercise - Implement polymorphism in a C# app
4+
metadata:
5+
title: Exercise - Implement polymorphism in a C# app
6+
description: "Exercise - Implement polymorphism in a C# app"
7+
ms.date: 03/06/2025
8+
author: wwlpublish
9+
ms.author: cahowd
10+
ms.topic: unit
11+
durationInMinutes: 30
12+
content: |
13+
[!include[](includes/6-exercise-implement-polymorphism-csharp-app.md)]
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.implement-polymorphic-behavior.knowledge-check
3+
title: Knowledge check
4+
metadata:
5+
title: Knowledge check
6+
description: "Knowledge check"
7+
ms.date: 03/06/2025
8+
author: wwlpublish
9+
ms.author: cahowd
10+
ms.topic: unit
11+
durationInMinutes: 4
12+
quiz:
13+
title: "Check your knowledge"
14+
questions:
15+
- content: "What is the purpose of using inheritance-based polymorphism in C#?"
16+
choices:
17+
- content: "To create a hierarchy of classes where derived classes inherit behavior and properties from a base class, allowing code to work with multiple types of objects without knowing the specific type at compile time."
18+
isCorrect: true
19+
explanation: "Correct. Inheritance-based polymorphism enables treating objects of derived classes as objects of the base class, allowing code to work with multiple types of objects without knowing the specific type at compile time."
20+
- content: "To write code that only works with one specific type of object at compile time."
21+
isCorrect: false
22+
explanation: "Incorrect. Inheritance-based polymorphism allows code to work with multiple types of objects without knowing the specific type at compile time."
23+
- content: "To prevent derived classes from inheriting behavior and properties from a base class."
24+
isCorrect: false
25+
explanation: "Incorrect. Inheritance-based polymorphism actually allows derived classes to inherit behavior and properties from a base class."
26+
- content: "What is the main advantage of using interface-based polymorphism over inheritance-based polymorphism?"
27+
choices:
28+
- content: "It promotes code reuse by sharing common behavior across multiple classes."
29+
isCorrect: false
30+
explanation: "Incorrect. Sharing common behavior across multiple classes is a benefit of inheritance-based polymorphism, not interface-based."
31+
- content: "It establishes a clear hierarchical relationship between classes."
32+
isCorrect: false
33+
explanation: "Incorrect. Establishing a clear hierarchical relationship between classes is an advantage of inheritance-based polymorphism, not interface-based."
34+
- content: "It promotes loose coupling, enhances flexibility, and simplifies maintenance."
35+
isCorrect: true
36+
explanation: "Correct. Interface-based polymorphism promotes loose coupling, enhances flexibility, and simplifies maintenance by reducing dependencies between classes."
37+
- content: "What is the main purpose of implementing polymorphism in a C# application?"
38+
choices:
39+
- content: "To reduce the amount of code written."
40+
isCorrect: false
41+
explanation: "Incorrect. While polymorphism can lead to more efficient code, its primary purpose isn't to reduce the amount of code written."
42+
- content: "To allow objects of different types to be treated as objects of a common base type."
43+
isCorrect: true
44+
explanation: "Correct. Polymorphism in C# allows objects of different types to be treated as objects of a common base type, enhancing flexibility and maintainability."
45+
- content: "To increase the complexity of the code."
46+
isCorrect: false
47+
explanation: "Incorrect. Polymorphism doesn't aim to increase complexity, but rather to enhance flexibility and maintainability."
48+
- content: "What is the main disadvantage of using sealed classes and methods when implementing inheritance-based polymorphism in C#?"
49+
choices:
50+
- content: "Sealed classes and methods can't be inherited or overridden, which limits the ability to use polymorphism."
51+
isCorrect: true
52+
explanation: "Correct. If a class or method is sealed, it prevents further extension and customization, limiting the use of polymorphism."
53+
- content: "Sealed classes and methods increase the flexibility and extensibility of the code."
54+
isCorrect: false
55+
explanation: "Incorrect. Sealing a class or method prevents further extension and customization, limiting flexibility."
56+
- content: "Sealed classes and methods can be easily overridden, which can lead to confusion."
57+
isCorrect: false
58+
explanation: "Incorrect. Sealed classes and methods can't be overridden, which limits the ability to use polymorphism."
59+
- content: "What is the purpose of casting in the context of inheritance-based polymorphism in C#?"
60+
choices:
61+
- content: "Casting is used to change the value of an object."
62+
isCorrect: false
63+
explanation: "Incorrect. Casting doesn't change the value of an object; it changes the type of the object."
64+
- content: "Casting is used to convert an object of a derived class to another derived class."
65+
isCorrect: false
66+
explanation: "Incorrect. Casting is typically used to convert an object of a base class to a derived class or vice versa."
67+
- content: "Casting is used to convert an object of one type to another type, often when implementing polymorphism using inheritance hierarchies."
68+
isCorrect: true
69+
explanation: "Correct. Casting is the process of converting an object of one type to another type, which is often used when implementing polymorphism using inheritance hierarchies."
70+
- content: "In which scenario would inheritance-based polymorphism be more appropriate than interface-based polymorphism?"
71+
choices:
72+
- content: "When the developer needs to reduce code dependencies."
73+
isCorrect: false
74+
explanation: "Incorrect. Reducing code dependencies is a benefit of interface-based polymorphism."
75+
- content: "When the developer needs to establish a hierarchical relationship between classes and promote code reuse."
76+
isCorrect: true
77+
explanation: "Correct. Inheritance-based polymorphism is beneficial when the developer needs to establish a hierarchical relationship between classes and share common behavior."
78+
- content: "When the developer needs to decouple class dependencies."
79+
isCorrect: false
80+
explanation: "Incorrect. Decoupling class dependencies is a benefit of interface-based polymorphism."
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.implement-polymorphic-behavior.summary
3+
title: Summary
4+
metadata:
5+
title: Summary
6+
description: "Summary"
7+
ms.date: 03/06/2025
8+
author: wwlpublish
9+
ms.author: cahowd
10+
ms.topic: unit
11+
durationInMinutes: 3
12+
content: |
13+
[!include[](includes/8-summary.md)]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Polymorphism is one of the core concepts of object-oriented programming (OOP). Polymorphism enables objects of different classes to be treated as objects of a common base class, providing flexibility and reusability in code.
2+
3+
Suppose you're working at a non-profit company that's in the midst of a software update project. The application combines tightly coupled models with a loosely coupled reporting structure. You're tasked with implementing polymorphism in the application to enhance flexibility and maintainability. You need to understand how to implement polymorphic behavior using class inheritance and interfaces. This module guides you through these concepts and provides practical examples to help you apply them in your work.
4+
5+
The topics covered in this module include:
6+
7+
- Examine the principles of polymorphic behavior.
8+
- Explore inheritance-based polymorphism.
9+
- Explore interface-based polymorphism.
10+
- Review polymorphic design considerations.
11+
- Implement polymorphism in a C# app.
12+
13+
After completing this module, you're able to:
14+
15+
- Explain the principles of polymorphism in C#.
16+
- Implement polymorphism by using class inheritance.
17+
- Implement polymorphism by using interfaces.
18+
- Choose the approach to polymorphism that matches scenario requirements.
19+
- Implement inheritance-based and interface-based polymorphism in a C# app.
20+
21+
By the end of this module, you have a solid understanding of polymorphism in C# and you're able to implement polymorphism using class inheritance or interface implementation.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Polymorphism allows objects of different classes to be treated as objects of a common base class. This behavior enables you to write code that works with objects of multiple types without knowing the specific type at compile time.
2+
3+
For example, consider a base class `Shape` with derived classes `Circle` and `Rectangle`. You can create a list of `Shape` objects and add instances of `Circle` and `Rectangle` to it. When you iterate over the list, you can call the `Draw` method on each object without knowing the specific type of the object. This process is an example of polymorphic behavior in action.
4+
5+
## Approaches to polymorphism in C#
6+
7+
C# developers can implement polymorphism using either of the following approaches:
8+
9+
- Inheritance-based polymorphism: Class inheritance in C# enables a derived class to inherit members from a base class. Members of the base class can be overridden in the derived class to provide specific implementations. Creating a class hierarchy is a common way to implement polymorphism in C#.
10+
11+
- Interface-based polymorphism: An interface in C# defines a contract that classes can implement. When a class implements an interface, the class receives a set of properties and methods that it's required to implement. This requirement allows for polymorphic behavior, where an interface reference can direct objects of different classes to implement the set of properties and methods.
12+
13+
## Method overloading versus method overriding
14+
15+
Method overloading and method overriding are two common techniques for implementing method calls that produce different behaviors. Here's a comparison of the two techniques:
16+
17+
- Method overloading: Method overloading allows you to define multiple methods with the same name but different parameters in the same class. The method to be called is determined at compile time based on the method signature. Method overloading is often referred to as compile-time polymorphism.
18+
19+
- Method overriding: Method overriding allows a derived class to provide a specific implementation of a method that is already defined in its base class. The method to be called is determined at runtime based on the actual type of the object. Method overriding is an example of runtime polymorphism.
20+
21+
This training focuses on method overriding, which is a key aspect of polymorphism in C#.
22+
23+
## Principles of polymorphism in C#
24+
25+
Polymorphism is based on the following principles:
26+
27+
- Method Overriding: Method overriding allows a derived class to provide a specific implementation of a method that's already defined in its base class. This capability is made possible by inheritance and is a key aspect of runtime polymorphism.
28+
- Virtual and Abstract Methods: The virtual keyword is used to declare methods in the base class that can be overridden in derived classes. The abstract keyword is used for declaring methods that must be overridden in derived classes.
29+
- Base Class References: Polymorphism allows a base class reference to point to objects of derived classes. This inheritance mechanism enables the invocation of overridden methods in derived classes through the base class reference.
30+
- Interface-Based Polymorphism: Interfaces define a contract that classes can implement. This contract allows different classes to be treated uniformly through a common interface, promoting loose coupling and flexibility.
31+
32+
## Benefits of polymorphism
33+
34+
- Code Reusability: Polymorphism promotes code reusability by allowing the same code to work with different types of objects.
35+
36+
- Flexibility and Maintainability: Polymorphism enhances flexibility and maintainability by decoupling the code that uses objects from the code that defines the objects.
37+
38+
- Extensibility: Polymorphism makes it easier to extend the system by adding new classes that implement the same interface or inherit from the same base class.
39+
40+
## Summary
41+
42+
Polymorphism is a key concept in object-oriented programming that enables flexibility, reusability, and maintainability in code. By understanding the principles of polymorphism and its implementation in C#, you can create modular and extensible applications that are easier to maintain and evolve over time.

0 commit comments

Comments
 (0)