Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Access Modifiers Practice | OOP Essentials
C# Beyond Basics

bookAccess 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:

  1. Making both Student and Teacher inherit from thePerson class;
  2. Ensure that the name field is accessible in the child classes but inaccessible from anywhere else.
index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
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 { public static void Main(string[] args) { Teacher t = new Teacher("Hannah"); Student s = new Student("Mark"); t.Teach(); s.Study(); } }

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain why the 'protected' access modifier is used for the 'name' field?

What would happen if the 'name' field was marked as 'private' instead of 'protected'?

Can you show how to add more functionality to the Student or Teacher classes?

Awesome!

Completion rate improved to 2.04

bookAccess Modifiers Practice

Scorri per mostrare il menu

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:

  1. Making both Student and Teacher inherit from thePerson class;
  2. Ensure that the name field is accessible in the child classes but inaccessible from anywhere else.
index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
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 { public static void Main(string[] args) { Teacher t = new Teacher("Hannah"); Student s = new Student("Mark"); t.Teach(); s.Study(); } }

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 4
some-alt