Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Intermediate C# Polymorphism Accessor Access Modifiers

Daniel Hildreth
Daniel Hildreth
16,170 Points

Confused on why we put get and took out const when making the tower class overridden.

I am confused on why we took out the const and put get in the override when we changed the Tower.cs. Originally the code was as below:

      private const int _range = 1;
      private const int _power = 1;
      private const double _accuracy = .75;

Then the code was changed to the following in order to override.

      protected virtual int Range { get; } = 1;
      protected virtual int Power { get; } = 1;
      protected virtual double Accuracy { get; } = .75;

Can someone explain why these changes happened this way?

1 Answer

Steven Parker
Steven Parker
231,268 Points

It looks like you answered your own question.

The answer is "in order to override". You can only override properties that are defined in the base class as "virtual". And adding the "get" makes it a property instead of a field, which is also necessary to override. You can't override fields.

Daniel Hildreth
Daniel Hildreth
16,170 Points

Ah that's why. I didn't realize you couldn't override fields.