Skip to content

Commit e24f9fa

Browse files
ClémentClément
authored andcommitted
Fixing polymorphism notes.
1 parent 75edfab commit e24f9fa

4 files changed

Lines changed: 27 additions & 30 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Class2 : Class1
2+
{
3+
public string Property2 { get; set; }
4+
5+
public Class2(string p1P, string p2P, string a1P){
6+
base.SetAttribute1(a1P);
7+
// The following would not work.
8+
// attribute1 = a1P;
9+
base.Property1 = p1P;
10+
Property2 = p2P;
11+
}
12+
}

source/code/projects/Polymorphism1/Polymorphism1/ClassHelper.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

source/code/projects/Polymorphism1/Polymorphism1/Program.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ static void Main()
66
object1.SetAttribute1("Test");
77
object1.Property1 = "Test";
88

9-
Class2 object2 = new Class2();
10-
object2.SetAttribute1("Test");
11-
object2.Property1 = "Test";
12-
object2.Property2 = "Test";
13-
14-
ClassHelper.Method1(object1);
15-
ClassHelper.Method1(object2);
16-
ClassHelper.Method2(object2);
9+
Class2 object2 = new Class2("property1", "property2", "attribute1");
10+
object2.SetAttribute1("Attribute1");
11+
object2.Property1 = "Property1";
12+
object2.Property2 = "Property2";
1713
}
1814
}

source/lectures/oop/polymorphism.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ While the example below is abstract, it can be easily instantiated to e.g., a `C
1616

1717
Consider the following two classes:
1818

19-
```
19+
```{download="./code/projects/Polymorphism1.zip"}
2020
!include code/projects/Polymorphism1/Polymorphism1/Class1.cs
2121
```
2222

23-
```
23+
```{download="./code/projects/Polymorphism1.zip"}
2424
!include code/projects/Polymorphism1/Polymorphism1/Class2.cs
2525
```
2626

@@ -43,32 +43,35 @@ Note, however, that
4343

4444
Stated differently, an object in `Class2` *is a(n object in)* `Class1`, but the converse is not true: an object in `Class1` *is not* an object in `Class2`.
4545

46+
47+
In addition, observe that the `Class2` constructor *cannot* access `class1`'s `attribute1` directly, since it is marked as `private`: it needs to use the `SetAttribute1` method.
48+
4649
## Polymorphism and References
4750

4851
Note that a `Class1` object can be created using a `Class2` constructor, since an object in `Class2` *is a(n object in)* `Class1`.
4952
Formally, we can write:
5053

5154
```
52-
Class1 object3 = new Class2();
55+
Class1 test = new Class2("property1", "property2", "attribute1");
5356
```
5457

55-
and then manipulate `object3` like any other *object from `Class1` (it is, in a way, "truncated").
58+
and then manipulate `test` like any other *object from `Class1` (it is, in a way, "truncated").
5659
In particular, we can use
5760

5861
```
59-
object3.Property1 = "Test";
62+
test.Property1 = "Test";
6063
```
6164

62-
but `object3.Property2 = "Test";` would not compile *since we would be trying to access a property of `Class2` with a `Class1` object.*
63-
Remember that an object in `Class1` *is not* an object in `Class2`, and that the way we declared it, `object3` *is* a `Class1` object.
65+
but `test.Property2 = "Test";` would not compile *since we would be trying to access a property of `Class2` with a `Class1` object.*
66+
Remember that an object in `Class1` *is not* an object in `Class2`, and that the way we declared it, `test` *is* a `Class1` object.
6467

6568
## Solving Ambiguity by Overriding
6669

6770
### For Methods
6871

6972
Now, consider the following class implementation and usage:
7073

71-
```
74+
```{download="./code/projects/Polymorphism2.zip"}
7275
!include code/projects/Polymorphism2/Polymorphism2/Class.cs
7376
```
7477

0 commit comments

Comments
 (0)