Help for Objective C Starter - Self and Super

Discussion in 'Mac Programming' started by Flamehearted, Aug 17, 2010.

  1. Flamehearted macrumors newbie

    Joined:
    Aug 4, 2010
    #1
    Hi all,

    Would anyone be able to give me an idiot proof description of Self and Super in objective C - I am struggling to understand these two from what I have read so far.

    Thanks in advance,

    Martin
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    self refers to the "current" instance of the class in question. It is a pointer back to the object. super refers to the instance of the superclass (the class this class inherits from). Note that these two instances are actually one object but the super pointer allows us to execute overridden methods.
     
  3. mrbash macrumors 6502

    Joined:
    Aug 10, 2008
    #3
    super is an aspect of Polymorphism in OO speak.

    If Object B is derived from Object A, calling super from an instance of Object B would get you the same instance but all of your methods and attributes would be from Object A. So if Object B overrides any methods from Object A, calling "super" would allow you to get the behavior from Object A.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    There is a description in this article, at the heading "Messages to self and super":
    http://developer.apple.com/mac/libr...#//apple_ref/doc/uid/TP30001163-CH12-BAJHDGAC

    • self searches for the method implementation in the usual manner, starting in the dispatch table of the receiving object’s class. In the example above, it would begin with the class of the object receiving the reposition message.
    • super starts the search for the method implementation in a very different place. It begins in the superclass of the class that defines the method where super appears. In the example above, it would begin with the superclass of the class where reposition is defined.
    You should read the complete original description, including its examples, then ask specific questions if something doesn't make sense.
     

Share This Page