abstract method equivalent for objective-c?

Discussion in 'iOS Programming' started by whitehexagon, Jun 14, 2009.

  1. whitehexagon macrumors regular

    whitehexagon

    Joined:
    May 12, 2007
    #1
    I have a base class with several child classes. I want to declare a method in the base class that all the extending classes are forced to implement. ie the base class itself has no implementation.

    Is this possible with objective-c, so far I'm only finding references to overiding the implementation from the base class which is not the same thing.
     
  2. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
  3. whitehexagon thread starter macrumors regular

    whitehexagon

    Joined:
    May 12, 2007
    #3
    Thanks! that looks almost like what I need.

    So I created a protocol definition in the header for the base class. My base class has the <MyProtocol> but this is forcing me to implement the protocol on the base class, but not the children. The reverse of what I wanted. What am I doing wrong, or is this the way protocol works?
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    I think you'd have to make the child objects conform to the protocol as well to force compile-time implementation of your method. Or just do some run-time stuff, like in the base class method throw an exception so that you can catch which child classes haven't implemented it.
     
  5. whitehexagon thread starter macrumors regular

    whitehexagon

    Joined:
    May 12, 2007
    #5
    Then is the protocol adding anything useful in this case? Sounds the same as just having a normal exception throwing method in the base class, and relying on the child classes to overide the method.

    Is there some other way to implement an abstract method in the base class?
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    I don't think so. I've never seen a design where a protocol was used to call a derived class. It's normally used as part of implementing the delegate pattern.

    Obj-C just doesn't have the same thing as a pure virtual method in C++.
     
  7. whitehexagon thread starter macrumors regular

    whitehexagon

    Joined:
    May 12, 2007
    #7
    Thanks, I can stop hunting around then. I'm sure someone could make a lot of money with a book ' Objective-c for Java programmers' :)
     
  8. jpyc7 macrumors 6502

    Joined:
    Mar 8, 2009
    Location:
    Denver, CO
    #8
    I wish there was a reference table that listed various concept names for different programming languages. Concepts like: abstract virtual methods in C++, interfaces in Java, Protocols in Obj-C, Categories in Obj-C, constructors in C++, etc and how they compare to each other.
     
  9. whitehexagon thread starter macrumors regular

    whitehexagon

    Joined:
    May 12, 2007
    #9
    Actually wikipedia is not the worst source for such comparisons, it's just too brief.
     
  10. vasile macrumors newbie

    Joined:
    Jun 15, 2009
    #10
    There is a good comparison of C++ with Obj-C at http://ktd.club.fr/programmation/fichiers/cpp-objc-en.pdf
     

Share This Page