I've got this Abstract class and I want some other class to inherit from it.
public string name; private int age; private int length; public int length { get { return length; } set { length = value; } } } class Oak: Tree { public Oak(string name, int age, int lengthInput) { this.name = name; this.age = age; length = lengthInput; } |
The problem is with setting the value of these variables in the Oak class.
this.name = name - doesn't give me errors, because I stated in the Tree class that name is public.
this.age = age - doesn't work, it says Tree.age is inaccessible due to it's protection level.
length = lengthInput - doesn't give me errors.
So If anyone can explain to me what do these things do?
I think I maybe overprotective with my variables. When should I use private keyword? At this point I have marked all private and created get/set declarations.
Thank you
Felipe RamosPosted Sep 20, 2010, 8:34 AM
abstract class Tree{public string name;private int age;private int length;protected Tree(string name, int age, int length){this.age = age;this.length = length;this.name = name;}public int Age{get { return age; }}public int Length{get { return length; }}}class Oak : Tree{public Oak(string name, int age, int lengthInput): base(name, age, lengthInput){}}In this particular case you are making sure your abstract class in setting your fields. You can also add the set in both properties and set the fields in the Oak constructor.Moose PickardPosted Sep 20, 2010, 8:27 AM
Manikavelu VelayuthamPosted Sep 20, 2010, 6:37 AM