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

iOS Object-Oriented Swift Value vs Reference Types Final Exam - Assignment

Scott Odle
Scott Odle
1,479 Points

Why accept a "sides" parameter for the square at all? Squares always have 4 sides.

Doesn't make much sense.

Mahdi Pedramrazi
Mahdi Pedramrazi
2,505 Points

I think logically the initializer for the Square class needs to accept only sideLength as a parameter. Since all Square shape object shave the same name and sides.

    init(sideLength: Double){
        self.sideLength = sideLength
        super.init(sides: 4, name: "Square")
    }

4 Answers

Yep - true! But that shouldn't get in the way of getting the assignment challenge complete though, eh?! ;-)

Steve.

haha

Jared Watkins
Jared Watkins
10,756 Points

A square will always have 4 sides, but other shapes will have other numbers of sides. So, the property 'sides' should be declared in the base class 'Shape' and it should be a constant. Doing it this way makes the 4-sidedness a property of a square. This will translate nicely to another object, too.

Hi Jared,

I'm not sure we want all instances of shape to have 4 sides, or do I misunderstand you?

Steve.

Jared Watkins
Jared Watkins
10,756 Points

Hi Steve,

Yeah, I didn't say that very clearly.

The sides property should be in the Shape class since all shapes have some number of sides, including even 1 or 0. Initializing the number of sides to 4 should happen in the convenience initializer for the Square subclass since a square will always have 4 sides.

This way, not all instances of Shape will have 4 sides - only the Squares will.

This will be an interesting assignment