C# C# Objects Inheritance Inheritance

Jordan Fotouhi
Jordan Fotouhi
7,058 Points

Class Square Inheriting from Polygon

I can't figure out what I'm doing wrong and what, if any, of the code I've done is correct. "The Polygon class represents a 2 dimensional shape. It has a public field named NumSides. Create a new type of polygon called Square. It should have a public readonly field named SideLength that’s initialized using the constructor. Use base to initialize NumSides to 4."

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

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

    class Square : Polygon
    {
        public readonly int SideLength(int numsides) : base(4)
        {

        }
    }
}

1 Answer

Steven Parker
Steven Parker
177,727 Points

You're actually pretty close, but you seem to have mixed the field declaration in with the constructor definition. Take a look at "Polygon" and use it as a model. Define your field "SideLength" on a line by itself, then define the contstructor for "Square" (constructors always have the same name as the class).

Also, while "Polygon" has a parameter named "numSides", yours will be "sideLength". You have the "base" call right, that throws folks a lot.

Finally, inside the constructor, set your field using the parameter similarly to what "Polygon" does.