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 trialTravis John Villanueva
5,052 PointsInheritance question
May be i didnt understand the question. Can somebody help me understand the question?
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)
{
}
}
}
Travis John Villanueva
5,052 PointsHi 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
25,513 PointsOK 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
5,052 PointsHi 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
25,513 PointsYou 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.
Ford Heacock
18,068 PointsFord Heacock
18,068 PointsYou'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!