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# C# Objects Inheritance Inheritance

Kelsey Opalinski
Kelsey Opalinski
4,405 Points

Did you define your Square class as an inner class of the Polygon class? I cant reason this one out :(

My square class is derived from the polygon class and is defined right after the polygons scope. i have move the square class inside the polygons scope, before the scope, and after the scope. all have not worked. what am i missing here?

Polygon.cs
namespace Treehouse.CodeChallenges
{
    class Polygon
    {
        public readonly int NumSides;

        public Polygon(int numSides)
        {
            NumSides = numSides;
            numSides = 4;
        }
    }
    class Square : Polygon
    {
        public readonly int SideLength
            public Square(int sideLength) : base (NumSides)
        {

        }

    }
}

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,736 Points

Polygons could have any number of sides. Squares are a type of polygon that have exactly 4 sides.

So you shouldn't change anything in the Polygon class (so you shouldn't be setting NumSides to 4 on line 10). What we're trying to do is define the Square class so that all squares will have 4 sides. On line 16 where you're calling base, this is where we want to be passing in the number 4, saying all squares will always have 4 sides. (And FYI we don't have access to the NumSides parameter in this scope here).

namespace Treehouse.CodeChallenges
{
    class Polygon
    {
        public readonly int NumSides;

        public Polygon(int numSides)
        {
            NumSides = numSides;
        }
    }

    class Square : Polygon 
    {
        public readonly int SideLength;
        public Square(int sideLength) : base(4)
        {
        SideLength = sideLength; 
        }

    }
}