Access Modifiers Practice
You are given a class named Person
with a field to store the name
. Additionally, there are two classes named Student
and Teacher
which derive from Person
.
There are some errors in the program because of some incomplete code. Your task is to complete the code by:
Making both
Student
andTeacher
inherit from thePerson
class;Ensure that the
name
field is accessible in the child classes but inaccessible from anywhere else.
index
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
public class Person
{
// Field to store the name
string name;
}
public class Student
{
public Student(string name)
{
this.name = name;
}
public void Study()
{
Console.WriteLine($"{name} is studying.");
}
}
public class Teacher
{
public Teacher(string name)
{
this.name = name;
}
public void Teach()
{
Console.WriteLine($"{name} is teaching.");
}
}
public class Program
12345678910111213141516171819202122232425262728293031323334353637383940414243444546using System; public class Person { // Field to store the name string name; } public class Student { public Student(string name) { this.name = name; } public void Study() { Console.WriteLine($"{name} is studying."); } } public class Teacher { public Teacher(string name) { this.name = name; } public void Teach() { Console.WriteLine($"{name} is teaching."); } } public class Program { public static void Main(string[] args) { Teacher t = new Teacher("Hannah"); Student s = new Student("Mark"); t.Teach(); s.Study(); } }
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 4. Hoofdstuk 4
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.