Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 16, 2017, 09:24:14 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Who is the parent here?  (Read 1662 times)
josf
Newbie
*
Posts: 8



« on: April 14, 2009, 07:22:00 AM »

I'm working in chapter 7 and looking ahead to the next, "Extension Through Inheritance . . ."

It seems logical enough that a square is a special case of a rectangle, and therefore that "Square" can be a subclass of "Rectangle". However, it seems to me that the rectangle class already has the methods that allow it to make the square in the first place. Just send the same value to setWidth and setHeight, no? Of course, I can see that the Square class iswhat would you say, more efficient? But on the other hand, aren't you just creating two objects where one would suffice?It seems odd to me that the parent class Rectangle is more flexible than its subclass. Why shouldn't it be the other way around? In other words, why wouldn't Square be the parent, having a method that takes one value, and Rectangle inherit that method. Then you could add the method to Rectangle that sets the Width and Height separately, couldn't you?
Logged

Joseph,
The Universe is in Cut Time
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #1 on: April 14, 2009, 07:41:11 AM »

Joseph,

As you know, a square is a special type of rectangle.  That implies the order of inheritance: The specialized class is a subclass of the more general class.  A square has all sides the same, implying that only one side needs to be specified (that is, only one instance variable is needed to store the information about a Square's dimensions).  If a Rectangle inherited from a Square, you wouldn't be able to specify both a width and height as separate dimensions, as the Square class would only have one instance variable for storing its information and all its methods for performing calculations (like area and perimeter) would just need to use that single value.

When you allocate a Square object, you are not creating two objects, just one.  The fact that the Square inherits from the Rectangle class just means it gets its instance variables and inherits its methods.  But it's still just a single object.

Hope that's clear.

Cheers,

Steve Kochan
« Last Edit: April 14, 2009, 07:44:18 AM by skochan » Logged
josf
Newbie
*
Posts: 8



« Reply #2 on: April 14, 2009, 08:38:32 AM »

Steve,

Thanks for the reply . . .

Okay, so if I understand it correctly, you're saying that the implied order of inheritance is based on the fact that the square inherits the setWidth and setHeight methods from Rectangle, is that right? And then we just add the method that sets them both to the same value? Have I got that right now?

And I also see the mistake I made in asking if we're creating two "objects" where one would suffice. I could re-phrase the question like this: Why shouldn't I just create a square with the Rectangle class like so: from the example, Program 8.2

Rectangle *aSquare = [[Rectangle alloc] init];

[aSquare setWidth: 5 andHeight: 5]; etc . . . ?


Logged

Joseph,
The Universe is in Cut Time
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #3 on: April 14, 2009, 09:05:23 AM »

 
Quote
Have I got that right now?

Yep!

Quote
Why shouldn't I just create a square with the Rectangle class like so: from the example, Program 8.2

Rectangle *aSquare = [[Rectangle alloc] init];

[aSquare setWidth: 5 andHeight: 5]; etc . . . ?

You could do that.  But isn't it nicer to have a Square class and just deal with a single dimension?  You don't lose anything by subclassing the Rectangle, and you gain added readability in your program as a result.  Someone seeing the setWidth:andHeight: method call would have to realize that the width and the height are the same and then draw the conclusion you're dealing with a square. 

Cheers,

Steve Kochan

[/quote]
Logged
josf
Newbie
*
Posts: 8



« Reply #4 on: April 14, 2009, 10:03:23 AM »

Okay . . . so subclassing Rectangle to make the Square is sort of like a luxury that we can afford :-) . . .

Thanks for those replies --------
Logged

Joseph,
The Universe is in Cut Time
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.