Please login to save your progress.
Visibility and Accessibility Modifiers
Exercise: Use the protected modifier
Goal
Modify the example classes
- Add a protected property to the class 'Student' named 'GraduationYear' of type int.
x
1
using System;
2
3
public class Student
4
{
5
public Student(int graduationYear)
6
{
7
GraduationYear = graduationYear;
8
}
9
10
public int GetYearsSinceGraduation()
11
{
12
return DateTime.Now.Year - GraduationYear;
13
}
14
}
15
16
public class Graduate : Student
17
{
18
public Graduate(int graduationYear)
19
: base(graduationYear)
20
{
21
22
}
23
24
public void GraduateToday()
25
{
26
GraduationYear = DateTime.Now.Year;
27
}
28
}
29
30
public class Program
31
{
32
public static void Main()
33
{
34
var student = new Student(2006);
35
Console.WriteLine("Years since gradation: {0}", student.GetYearsSinceGraduation());
36
}
37
}
Page 7 of 18