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 trialJordan Fotouhi
7,058 PointsClass 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."
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
231,261 PointsYou'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.