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

Daniel W
Daniel W
1,341 Points

Stuck on Polygon/Square class inheritence challenge in C#.

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

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

}

class Square : Polygon
{
public readonly int SideLength;

    public Square(sideLength) : base(4)
    {
        SideLength = sideLength;
    }

}

}

The task is to create a Square that inherits from the Polygon class, and give it 4 sides. I've used base(4) to specifty that, create a new class in the namespace that inherits the properties of the Polygon. What am I doing wrong here?

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

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

    }

    class Square : Polygon
    {
    public readonly int SideLength;

        public Square(sideLength) : base(4)
        {
            SideLength = sideLength;
        }

    }

}

2 Answers

Steven Parker
Steven Parker
231,271 Points

So close — you nearly had it!

:point_right: You forgot to declare the type of the argument "sideLength".

Daniel W
Daniel W
1,341 Points

Well, then I have another question. When you call base(4), why aren't you supposed to type that it's an int? For example base(int 4)... wouldn't that make it more clear for those who read the code? Or if I called the function:

OnMap(Point somepoint)

Wouldn't that be better than just

OnMap(somepoint)?

And if there are two method names where one takes an int and another takes a double, how can the code determine which method to call? For example if I had a method "add" that takes in two doubles and another method with the same name that takes two ints. If I call add(4,5) how does it know to call the int-version over the double version?