Amazon.com Widgets polymorphism ?
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 22, 2014, 07:57:17 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 9 - Polymorphism, Dynamic Typing, and Dynamic Binding
| | | |-+ polymorphism ?
Pages: [1] Go Down
Print
Author Topic: polymorphism ? (Read 2880 times)
David Mantripp
Newbie
*
Posts: 3






on: April 02, 2009, 08:56:43 AM

Hi -

My first post here, and I have to apologise because it is cross-posted from amazon.com, but I only discovered this forum afterwards....

As somebody who can vaguely remember using C on Solaris in the early 90s, and never really felt comfortable with Java, I have to say I'm really enjoying this book, and finding that Objective C is the first OO language I've encountered that feels natural.

However, I've just hit one definition that leaves me baffled: in the section in Chapter 9 on Polymorphism, there is the statement "polymorphism enables programs to be developed so that objects from different classes can define methods that share the same name". I really don't get this. I thought that I had at least a rough grasp of what polymorphism is, but surely this statement is just telling me I can define methods with the same name for 2 or more unrelated classes - so what ?

I realise that all becomes clear later, but it is just this definition which gives me an uneasy feeling that I'm missing something obvious at a critical point :-)

Thanks
David
Logged
jclermont
Jr. Member
**
Posts: 55


Email




Reply #1 on: April 02, 2009, 11:08:27 AM

I'm learning this myself right now, but I thought I'd try to help out. Someone else can correct me if I'm inaccurate.

It's not just that the methods happen to have the same name. It's more powerful than that. It allows you to send the same message to objects of different types and have them do the right thing. So for example if you create a method that accepts an object as an input parameter, you don't have to create all kinds of different cases based on what type that object is. Here's a more concrete example (OO pun):

Your method accepts an input parameter of any type NSObject. In your program that object might be a Triangle type or a Circle type or a Square type. One approach would be in your method to test for the type, then send your object a different message based on its type. With polymorphism you can define an interface that is shared between a Triangle, Circle and Square. So you could safely call [inputObject draw] instead of trying to figure out the type and calling the right method.

I hope that makes sense  Smiley
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #2 on: April 02, 2009, 11:36:43 AM

I couldn't have said that better myself!   Wink

Cheers,

Steve Kochan
Logged
David Mantripp
Newbie
*
Posts: 3






Reply #3 on: April 02, 2009, 12:34:07 PM

Hmmm...

So does this mean that if I did not define an "add" method in Complex, then [c1 add: c2] would result in an attempt to use the "add" method in Fraction and fail ?

I suppose I could just suck it and see :-)     Well, it can wait until tomorrow.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #4 on: April 02, 2009, 12:49:17 PM

If c1 is a Complex object, then the compiler will expect to see an add: method defined for the Complex class; it would never use the Fraction's method instead.

Att runtime (and this is one of the key features of Objective-C) the class of the object will be determined and the appropriate method from that object's class will be called.   If there is no such method (either explicitly defined in the class or inherited by the class), you will get a runtime exception error.

Cheers,

Steve
Last Edit: May 20, 2009, 09:40:32 AM by skochan Logged
David Mantripp
Newbie
*
Posts: 3






Reply #5 on: April 03, 2009, 12:50:44 AM

Thanks Steve / jclermont - this forum is really the icing on the cake for an excellent book.

I think I was getting a bit stuck with hazy memories of overloading in C++.  I actually went back to Bertrand Meyer's 1968 book on OOP last night to get back to basics ... and that cleared up a few things as well, including the idea of dynamic binding.

So I'm cleared up and diving back into XCode
Logged
john67
Newbie
*
Posts: 32






Reply #6 on: August 11, 2011, 03:09:14 PM

Just extending this then, suppose the complex class 'add' method is set to receive ints and instead we use doubles - does this require a separate method?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #7 on: August 11, 2011, 04:20:35 PM

As long all other add: methods are defined to take int's.  The double will be converted to an int (truncated) when passed to the method.

Cheers,

Steve
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.