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
MOD
Brendan Whiting
Front End Web Development Treehouse Moderator 84,129 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; 
        }

    }
}