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 Virtual Properties

Atanu Mondal
Atanu Mondal
1,976 Points

if I use protected to the property and then override why is not compiling ?

I am able to override the property with a public modifier but why is it not compiling with protected as repeatDetector is child class. as per Microsoft doc, it should work, right?

"A protected member is accessible within its class and by derived-class instances."

RepeatDetector.cs
namespace Treehouse.CodeChallenges
{
    class RepeatDetector : SequenceDetector
    {
        protected override string Description => "Detects repetitions";

        public override bool Scan(int[] sequence)
        {
            if(sequence.Length < 2)
            {
                return false;
            }

            for(int i = 1; i < sequence.Length; ++i)
            {
                if(sequence[i] == sequence[i-1])
                {
                    return true;
                }
            }

            return false;
        }
    }
}
SequenceDetector.cs
namespace Treehouse.CodeChallenges
{
    class SequenceDetector
    {
        protected virtual string Description => "";

        public virtual bool Scan(int[] sequence)
        {
            return true;
        }
    }
}

1 Answer

Steven Parker
Steven Parker
229,708 Points

You should've gotten this message: "Bummer: Is the getter of the 'RepeatDetector.Description' property public?"

That's the challenge hinting that you need to make the property public instead of protected. You're right about the access of a protected property, but this one needs to be accessible by code (which is not shown here) that is outside of the class and its base.

The need for public access is implied by what the property contains, as a description of what the object is for would obviously not be used within the object itself.