Jesper Jesper - 1 year ago 74
C# Question

Getting fields from superclass

I have the following abstract class:

abstract class Customer
{
private string address { get; set; }
private int phone { get; set; }

public Customer(string address, int phone)
{
this.address = address;
this.phone = phone;
}
}


Then i have the following class that inherits from the customer class:

class Private : Customer
{

private string name { get; set; }
private int age { get; set; }
private string sex { get; set; }

public Private(string name, int age, string sex, string address, int phone) : base(address, phone)
{
this.name = name;
this.age = age;
this.sex = sex;
}


My question is: How do I access the phone and address fields on my Private class?

Answer Source

Use the protected modifier instead of private for everything that can be accessed by inherited classes.

From the docs:

A protected member is accessible within its class and by derived class instances.

https://msdn.microsoft.com/en-us/library/bcd5672a.aspx