3
Answers

Setting variable values in constructor, help

Moose Pickard

Moose Pickard

14y
1.9k
1
Hey 

I've got this Abstract class and I want some other class to inherit from it.

  abstract class Tree {
         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

Answers (3)