Why i can't access protected property in the derived class

user3174121

I have a two classes.This is my code:

//My Base class
public class People
    {
       public People()
       {
       }

        protected string name;

        protected string Name
           {
        get 
        {
            return this.name;
        }
        set 
        {
            this.name = value;
        }
    }

    }

//The Child class 

public class Student:People
    {
        private int id;

        public Student()
        { 
        }

        public Student (int id, string name)
        {
            this.id = id;
            this.Name = name;
        }

        public int ID
        {
            get
            {
                return this.id;
            }

            set

            {
                this.id = value;
            }

        }
    }

When i create instance of the Student class like the one below i can't access the NAME property from the parent class People.

 public partial class Form1 : Form
    {

        private void button1_Click(object sender, EventArgs e)
        {
            Student student1 = new Student();
            student1. // only ID property is accessible 

        }
    }

Do i make something wrong? Since Students is child class of People i expected that the NAME property should be accessible trough the Student instances.Thank you very much for the help in advance.

Omri Aharon

You're not doing anything wrong, but if you want to access

Name

via an instance of

Student

You have to declare that property public. Otherwise, only access from within that class is allowed (not via an instance).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why a derived class can't access a protected getter from the base class?

From Dev

Derived class can't access protected method of base class

From Dev

Why can't I access inherited protected fields in a derived constructor's member initialization list?

From Java

Why can't a derived class call protected member function in this code?

From Dev

Why can't we call protected destructors from a derived class?

From Dev

Why can't I access a protected member from an instance of a base class?

From Dev

Why `this` can't access the derived class members from base class methods when called for derived class object

From Dev

I can't access my class property

From Dev

Can't call virtual protected method in derived class

From Dev

Why can't I access a member of this class?

From Dev

Why can I access a protected method in a test?

From Dev

Why an instance of inherited class can't access to protected member of base class in different package

From Dev

Why an instance of inherited class can't access to protected member of base class in different package

From Dev

Why can parents access instances of child class' protected members, if siblings can't?

From Dev

Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

From Dev

Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

From Dev

Protected method access from derived class

From Dev

Cannot access protected members from a derived class

From Dev

why can't my class friend function access a protected member with namespaces?

From Dev

Why can't my object access protected members of another object defined in common base class?

From Dev

Why can't I assign a "protected" modifier to a "friend" property in VB.NET?

From Dev

Why can't I directly access a property of an object literal?

From Dev

Why can't I access meta from a property defined in setupController?

From Dev

Why can't I access to a UserControl custom property inside a Repeater?

From Dev

Why can't I access a property of this destinationViewController that conforms to a protocol?

From Dev

Why can't I access a protected method from a subclass in C#?

From Dev

Why can't I override the #at:put: method in a class derived from the Dictionary-class?

From Dev

Why can't I use alias from a base class in a derived class with templates?

From Dev

Why can't I customize the getter and setter if class property is atomic?

Related Related

  1. 1

    Why a derived class can't access a protected getter from the base class?

  2. 2

    Derived class can't access protected method of base class

  3. 3

    Why can't I access inherited protected fields in a derived constructor's member initialization list?

  4. 4

    Why can't a derived class call protected member function in this code?

  5. 5

    Why can't we call protected destructors from a derived class?

  6. 6

    Why can't I access a protected member from an instance of a base class?

  7. 7

    Why `this` can't access the derived class members from base class methods when called for derived class object

  8. 8

    I can't access my class property

  9. 9

    Can't call virtual protected method in derived class

  10. 10

    Why can't I access a member of this class?

  11. 11

    Why can I access a protected method in a test?

  12. 12

    Why an instance of inherited class can't access to protected member of base class in different package

  13. 13

    Why an instance of inherited class can't access to protected member of base class in different package

  14. 14

    Why can parents access instances of child class' protected members, if siblings can't?

  15. 15

    Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

  16. 16

    Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

  17. 17

    Protected method access from derived class

  18. 18

    Cannot access protected members from a derived class

  19. 19

    why can't my class friend function access a protected member with namespaces?

  20. 20

    Why can't my object access protected members of another object defined in common base class?

  21. 21

    Why can't I assign a "protected" modifier to a "friend" property in VB.NET?

  22. 22

    Why can't I directly access a property of an object literal?

  23. 23

    Why can't I access meta from a property defined in setupController?

  24. 24

    Why can't I access to a UserControl custom property inside a Repeater?

  25. 25

    Why can't I access a property of this destinationViewController that conforms to a protocol?

  26. 26

    Why can't I access a protected method from a subclass in C#?

  27. 27

    Why can't I override the #at:put: method in a class derived from the Dictionary-class?

  28. 28

    Why can't I use alias from a base class in a derived class with templates?

  29. 29

    Why can't I customize the getter and setter if class property is atomic?

HotTag

Archive