Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C equivalent of "override" in C#

Tags:

objective-c

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.

like image 863
Andrei Tanasescu Avatar asked Jun 23 '09 19:06

Andrei Tanasescu


People also ask

What is override in C?

When a derived class or child class defines a function that is already defined in the base class or parent class, it is called function overriding in C++. Function overriding helps us achieve runtime polymorphism.

Is Objective-C and C# the same?

Objective-C and C# are very different languages both syntactically and from a runtime standpoint. Objective-C is a dynamic language and uses a message passing scheme, whereas C# is statically typed.

What is override keyword in C#?

override (C# reference)An override method provides a new implementation of the method inherited from a base class. The method that is overridden by an override declaration is known as the overridden base method. An override method must have the same signature as the overridden base method.

Is Objective-C faster than C++?

Just like in C++, Objective-C relies in compiler-generated function pointer tables that are traversed at runtime. Unlike C++, however, the binding method is more 'dynamic', and promotes the use of the id superclass everywhere, making it slightly slower than C++ in theory.


2 Answers

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.

like image 107
Chris Hanson Avatar answered Oct 19 '22 03:10

Chris Hanson


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];
}
like image 12
Ben Gotow Avatar answered Oct 19 '22 02:10

Ben Gotow