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.
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.
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.
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.
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.
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.
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With