C # "unavailable due to its level of protection" error in constructor

The constructor of the Caesar child class gives an error. It indicates that the name, type is not available due to its level of protection. How so? Since this is a child class derived from the "Cipher" class, it should not give such an error. How can I overcome this situation. But I want these variables to be private. I do not want to change them as public.

*** The second code example works. Can anyone see the difference?

namespace Encrypter { class Cipher { public Cipher(string name, string type) { setName(name); setType(type); } private string name; private string type; public void setName(string newName) { name = newName; } public string getName() { return name; } public void setType(string newType) { type = newType; } public string getType() { return type; } public string encrypt(string text) { return text; } public string decrypt(string text) { return text; } } } namespace Encrypter { class Caesar : Cipher { private int shiftamount; private string shiftdirection; public Caesar(int shiftamount, string shiftdirection) : base(name, type) { setShiftamount(shiftamount); setShiftdirection(shiftdirection); } public void setShiftamount(int newShiftamount) { shiftamount = newShiftamount; } public int getShiftamount() { return shiftamount; } public void setShiftdirection(string newShiftdirection) { shiftdirection = newShiftdirection; } public string getShiftdirection() { return shiftdirection; } } } 

----------------------------- New Editing ----------------

 class MyFile { public MyFile(int id, string name, int size, string type) { setId(id); setName(name); setSize(size); setType(type); } private int id; private string name; private string type; private int size; class Movie : MyFile { private string director; private int release_year; public Movie(string director, int release_year, int id, string name, int size) : base( id, name, size, "m") { setDirector(director); setRelease_year(release_year); } 
+6
inheritance access-modifiers c # encapsulation
source share
4 answers

It looks like you were mistaken in defining the constructor of the derived class. If you want to get the name and type values ​​for the superclass, you will have to pass them as additional arguments to the constructor (there are 4 arguments in the constructor of the derived class). For example, changing it to this should work:

  public Caesar(int shiftamount, string shiftdirection, string name, string type) : base(name, type) 

There are several other strategies you could take.

+7
source share
  public Caesar(int shiftamount, string shiftdirection) : base(name, type) { 

The problem is the name and type of private fields - the child class cannot access them if they are not marked as protected . What you really want, I suspect

  public Caesar(int shiftamount, string shiftdirection) : base("Caesar5", "Caesar") { 
+5
source share

Private members cannot be accessed from derived classes. protected and public. You must protect them. Thus, only the class and its "children" will have access.

Permission Summary:

  • private: you can only access from these class methods, nothing more
  • protected: can be obtained from this class and its child methods
  • internal: only methods can be accessed within the same assembly
  • protected internal: same as internal + methods of derived classes from other assemblies
  • public: may be accessible to all
+3
source share

private means that only the declaration class can access members (which means that inherited types cannot too).

protected means that the declaring class and any descendants can access the elements, but types outside of them cannot.

In another note, the getter and setter functions are not commonly used in .NET languages. Properties encapsulate this functionality and are what should be used instead. For instance:

 private string name; public string Name { get { return name; } set { name = value; } } 
+3
source share

All Articles