Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Access Modifiers Practice | OOP Essentials
course content

Зміст курсу

C# Beyond Basics

Access Modifiers PracticeAccess 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.
cs

index.cs

1. In this task first you need to use the concept of derived classes, then the access modifiers.
2. We use the : symbol to specify the parent class in the definition of a child (derived) class.
3. Recall which access modifier is appropriate for the name field to hide it from the outside of the class but make it accessible from the child classes.

using System;

public class Person
{
    // Field to store the name
    protected string name;
}

public class Student : Person
{

    public Student(string name)
    {
        this.name = name;
    }

    public void Study()
    {
        Console.WriteLine($"{name} is studying.");
    }
}

public class Teacher : Person
{
    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();
    }
}  
    

Все було зрозуміло?

Секція 4. Розділ 4
course content

Зміст курсу

C# Beyond Basics

Access Modifiers PracticeAccess 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.
cs

index.cs

1. In this task first you need to use the concept of derived classes, then the access modifiers.
2. We use the : symbol to specify the parent class in the definition of a child (derived) class.
3. Recall which access modifier is appropriate for the name field to hide it from the outside of the class but make it accessible from the child classes.

using System;

public class Person
{
    // Field to store the name
    protected string name;
}

public class Student : Person
{

    public Student(string name)
    {
        this.name = name;
    }

    public void Study()
    {
        Console.WriteLine($"{name} is studying.");
    }
}

public class Teacher : Person
{
    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();
    }
}  
    

Все було зрозуміло?

Секція 4. Розділ 4
some-alt