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

Andreas Gruber
Andreas Gruber
2,142 Points

I don't understand the task, can you please explain it in other words... Greets Andi

where should I create the type? In a new class? Sorry I don't understand this :)

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

        public Polygon(int numSides)
        {
            NumSides = numSides;
        }
    }
}
Andreas Gruber
Andreas Gruber
2,142 Points

This is the question:

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.

1 Answer

Steven Parker
Steven Parker
231,198 Points

:mailbox_with_mail: I got your message.

Yes, you will be creating a new class named Square, which will inherit from Polygon. You might want to use the existing definition of the Polygon class as an example but remember your new class will have inheritance, and your constructor will explicitly call the base constructor and pass it a 4 for the number of sides.

Your Square will take the side length as an argument, just like Polygon takes the number of sides. It wlll also store it in a read-only variable.

Hopefully these hints will get you going. And don't hesitate to revisit the video if the concepts of inheritance are not clear yet.

Andreas Gruber
Andreas Gruber
2,142 Points

Thank you very much, it worked!!