Skip to content

Commit ab84a2f

Browse files
ClémentClément
authored andcommitted
Adding notes on protected members.
1 parent b84635d commit ab84a2f

5 files changed

Lines changed: 54 additions & 13 deletions

File tree

source/code/projects/InheritanceAndAccessModifiers/InheritanceAndAccessModifiers/FMember.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public FMember(string relationP, string nameP)
1313
// which exists to create first
1414
// a person object.
1515
Relationship = relationP;
16-
// the following
1716
// name = nameP;
18-
// gives
19-
// error CS0122: 'Person.name' is inaccessible due to its protection level
17+
// would give
18+
// 'Person.name' is inaccessible due to its protection level
2019
// so we have to use the public method instead:
2120
SetName(nameP);
22-
// the following
2321
// NewName(nameP);
24-
// gives
25-
// error CS0122: 'Person.NewName(string)' is inaccessible due to its protection level
22+
// would give
23+
// error CS0122: 'Person.NewName(string)' is inaccessible due to its protection level
24+
nickName = "Unknown";
25+
commonName = "Unknown";
2626
}
2727
}

source/code/projects/InheritanceAndAccessModifiers/InheritanceAndAccessModifiers/Person.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
public class Person
22
{
33
private string name;
4+
protected string nickName;
5+
public string commonName;
46

57
public string GetName()
68
{
79
return name;
810
}
911

10-
public Person() { }
11-
1212
public void SetName(string nameP)
1313
{
1414
if (name == null)
@@ -21,8 +21,10 @@ public void SetName(string nameP)
2121
}
2222
}
2323

24-
private void NewName(string newnameP)
24+
private void NewName(string newNameP)
2525
{
26-
name = newnameP + $" (previous name: {name})";
26+
name = newNameP + $" (previous name: {name})";
2727
}
28+
29+
public Person() { }
2830
}

source/code/projects/InheritanceAndAccessModifiers/InheritanceAndAccessModifiers/Program.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ public static void Main()
1010
test1.SetName("Patty");
1111
Console.WriteLine(test1.GetName());
1212

13-
FMember test = new FMember("Cousin", "Samuel");
13+
// test1.name = "Test";
14+
// would give
15+
// 'Person.name' is inaccessible due to its protection level
16+
17+
// test1.nickName = "Test";
18+
// would give
19+
// 'Person.nickName' is inaccessible due to its protection level
20+
21+
test1.commonName = "Test";
22+
FMember test2 = new FMember("Cousin", "Samuel");
23+
1424
}
1525
}

source/diag/cla/FMember.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Person <|-- FMember
2+
3+
class Person{
4+
- name: string
5+
# nickName: string
6+
+ commonName: string
7+
+ GetName(): string
8+
+ SetName(nameP: string)
9+
- NewName(newNameP)
10+
+ Person()
11+
}
12+
13+
class FMember{
14+
+ relationShip: string
15+
+ FMember(relationP: string, nameP:string)
16+
}

source/lectures/oop/inheritance.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,13 @@ Note that `SetNOW` inside the no-args constructor actually calls a `SetNOW` meth
7575
If there was a `SetNOW` method in both the `Bike` and the `Vehicle` method, we could not force access to the `Vehicle` method^[Actually, we could, if you read [this post](https://stackoverflow.com/a/32562464), but it is considered bad practice.].
7676
-->
7777

78-
## On Private and Public Methods and Attributes
78+
## On Private, Public and Protected Methods and Attributes
7979

80-
The following example tries to make it clear that `private` attributes *cannot* be accessed from classes inheriting.
80+
The following example tries to make it clear that
81+
82+
- `private` attributes *cannot* be accessed from classes inheriting, nor from e.g., `Main` methods manipulating objects,
83+
- `protected` attributes *can* be accessed from classes inheriting, but *cannot* be accessed from e.g., `Main` methods manipulating objects,
84+
- `public` attributes *can* be accessed from classes inheriting *and* from e.g., `Main` methods manipulating objects.
8185

8286
```{download="./code/projects/InheritanceAndAccessModifiers.zip"}
8387
!include code/projects/InheritanceAndAccessModifiers/InheritanceAndAccessModifiers/Person.cs
@@ -86,3 +90,12 @@ The following example tries to make it clear that `private` attributes *cannot*
8690
```{download="./code/projects/InheritanceAndAccessModifiers.zip"}
8791
!include code/projects/InheritanceAndAccessModifiers/InheritanceAndAccessModifiers/FMember.cs
8892
```
93+
94+
```{download="./code/projects/InheritanceAndAccessModifiers.zip"}
95+
!include code/projects/InheritanceAndAccessModifiers/InheritanceAndAccessModifiers/Program.cs
96+
```
97+
98+
99+
Protected attributes and methods are denoted with the `#` sign in UML diagrams, as follows:
100+
101+
!include diag/cla/FMember.md

0 commit comments

Comments
 (0)