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

Travis John Villanueva
Travis John Villanueva
5,052 Points

Inheritance question

May be i didnt understand the question. Can somebody help me understand the question?

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

        public Polygon(int numSides)
        {
            NumSides = numSides;
            Polygon square=new Square(4);
        }

    }




    class Square: Polygon
    {
      public readonly int SideLength;

      public Square (int sideLength): base(numSides)
      {

      }




    }    
}
Ford Heacock
Ford Heacock
18,068 Points

You're on the right track! Instead of initializing on the Polygon class, go down to the subclass you just created called Square. It's asking you to initiate the new field SideLength with a base of 4 calling from the original Polygon Class (the base). Remove the

Polygon square=new Square(4);

From the the first class and see if you can figure this out. You're close!

Travis John Villanueva
Travis John Villanueva
5,052 Points

Hi Ford, thanks for the response. I tried what you said earlier and put the initialisation on the square class. Yet nothing still happens. Ut gives me an error whether I did initialise it in the base structure.

2 Answers

luke hammer
luke hammer
25,513 Points

OK let's break down the task.

1.>Create a new type of polygon called Square

Done! 2.>It should have a public readonly field named SideLength.

Done! 3.>field named SideLength that’s initialized using the constructor

Need to do this there is currently no work being done in your Square constructor.

   class Square: Polygon
    {
      public readonly int SideLength;

      public Square (int sideLength): base(numSides)
      {
 /*this is were the side Length needs to be set to */
      }

4.>Use base to initialize NumSides to 4

Currently you are attempting to do this in your Polygon Constructor. Hint no thing needs to be added in Polygon.

calling the base constructor happens in the Square Class.

I know I'm not giving you the full answer but work with this and if you have more questions Post your need code here.

Good Luck

Travis John Villanueva
Travis John Villanueva
5,052 Points

Hi Luke, actually ive done what you said for the first time. When I still had edrors I tried it on the polygon class.

here is what I initialized in the square class. Square square = new Square(4);

did I miss something?

luke hammer
luke hammer
25,513 Points

You are working in classes so you should not be using the word "new" at all.

New Creates an instance of an object. Here we are just attempting to write instructions on how to build the object.

So New is not correct.