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); }
inheritance access-modifiers c # encapsulation
Alptugay
source share