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: learn-pr/wwl-azure/implement-polymorphic-behavior/includes/1-introduction.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
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.
1
+
Polymorphism is one of the core concepts of object-oriented programming. Polymorphism enables objects of different types to be treated as objects of a common base type, providing flexibility and reusability in code.
2
2
3
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.
Copy file name to clipboardExpand all lines: learn-pr/wwl-azure/implement-polymorphic-behavior/includes/2-examine-principles-polymorphism.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
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.
1
+
Polymorphism allows objects of different types to be treated as objects of a common base type. This behavior enables you to write code that works with objects of multiple types without knowing the specific type at compile time.
2
2
3
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
4
5
5
## Approaches to polymorphism in C#
6
6
7
7
C# developers can implement polymorphism using either of the following approaches:
8
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#.
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. When you create instances of the base class that reference the derived class types, you can access members of the derived class from the base class objects. Inheritance-based polymorphism is a common way to implement polymorphism in C#.
10
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.
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. Interface-based polymorphism is another common way to implement polymorphism in C#.
12
12
13
13
## Method overloading versus method overriding
14
14
@@ -24,16 +24,16 @@ This training focuses on method overriding, which is a key aspect of polymorphis
24
24
25
25
Polymorphism is based on the following principles:
26
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.
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
31
32
32
## Benefits of polymorphism
33
33
34
-
- Code Reusability: Polymorphism promotes code reusability by allowing the same code to work with different types of objects.
34
+
- Code reusability: Polymorphism promotes code reusability by allowing the same code to work with different types of objects.
35
35
36
-
- Flexibility and Maintainability: Polymorphism enhances flexibility and maintainability by decoupling the code that uses objects from the code that defines the objects.
36
+
- Flexibility and maintainability: Polymorphism enhances flexibility and maintainability by decoupling the code that uses objects from the code that defines the objects.
37
37
38
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.
0 commit comments