tags:

views:

1174

answers:

3

Assume a class Vehicle has a method called "StartEngine", and in my subclass called "Airplane" I want to override "StartEngine". If I use C#, I should use the "override" keyword in Airplane's StartEngine method declaration/definition.

The reason I am asking, is that if instead of "StartEngine" I type "startengine", C# will complain, but Objective-C won't, thanks to that keyword.

+4  A: 

Yep - it's definitely possible. There's no override keyword though. Just declare a function with an identical signature in your subclass, and it will be called instead of the superclasses version.

Here's what Airplane's startEngine method might look like:

- (void)startEngine
{
    // custom code

    // call through to parent class implementation, if you want
    [super startEngine];
}
Ben Gotow
I've just clarified the question. I know overriding is possible in objective-c, I just want the compiler to check I'm actually overriding a method, and not creating a new one in the subclass.
Andrei Tanasescu
Ahh - I see. Unfortunately, the Objective-C compiler doesn't provide any way to do that. Heck - even calling a function that doesn't exist is only a warning! :-) I think your best bet in that case would be to make the abstract base class' method implementations throw NSExceptions.
Ben Gotow
+6  A: 

All class and instance methods in Objective-C are dispatched dynamically and can be overridden in subclasses. There is nothing like C#'s override keyword, or like its virtual keyword.

Chris Hanson
That makes sense.. I've just learned what a method is in objective-c : a pointer to the actual function a name and an encoding... Nothing extra.
Andrei Tanasescu
Is it then also polymorphic?
Henri
All methods in Objective-C are polymorphic, including class methods. (This is unlike "static" methods in C#, Java, and C++, which are not polymorphic.)
Chris Hanson
Let me clarify: All methods in Objective-C are polymorphic in the sense that they can always be overridden by subclasses. No methods in Objective-C are polymorphic on argument type.
Chris Hanson
+1  A: 

All methods in Objective-C are overrideable, you just write a method for the same signature.

Marco Mustapic