Overload and Override Details in Objective C

  • 2021-06-29 12:11:45
  • OfStack

Objective-C overload and override

First, overloading is not fully supported in Objective-C. Many people on the Internet either mix up overloading with overloading or say that OC does not support overloading (although it is true that OC does not support overloading by the strict definition of overloading). In fact, OC supports function overloading with different number of parameters.

Question: Are there overloads in Objective-C and Swift?

There are overloads in Swift, but they are not supported in Objective-C at all.

Open:

Overload, Override, and Hide Definitions in Programming Languages

Overload (overload): Functions have the same name, different parameter lists (including number of parameters and parameter types), and different return types.Overloads can occur either between different functions of the same class or between inheritance relationships of parent and child classes, with the distinction between parent and child classes being noted and overridden.

Override (override): Occurs between a parent and a child class, meaning that a child class does not want to inherit methods that use the parent class and overrides the same function in the parent class by overriding the implementation of the same function, hence called function override.Note that the overridden function must be identical to the parent class 1, including the function name, number and type of parameters, and return value. It simply overrides the implementation of the function, which is the key to distinguishing between overload and overload.

Hide: Once overload and override are separated, hide may be mixed with the first two.Of course, there is no hiding in OC. In a typical C++, there is a distinction between virtual functions and function overloads between parent and child classes, which is not discussed here.Overload and override are for functions, while hiding is for member variables as well as functions.Hiding occurs between a parent class and a child class. Hiding refers to the hiding of functions or variables of the same name in a child class, where functions with the same name are hidden regardless of whether the parameters are the same or not.Functions or variables of the same name of the parent class are not visible in the subclass, but still exist in the parent class.

Swift is a new language optimized for use by C and OC. It overcomes the compatibility limitations of C, uses a secure programming mode and adds some new features to make programming more interesting and friendly, adapting to language trends and expectations.Function overload is supported in Swift as part of the polymorphism and may also be taken into account to compensate for this defect in OC which does not fully support function overload.OC does not fully support overloading, because OC learners should find that defining two functions with the same function name and the same number of parameters in the same class is not allowed, regardless of the parameter type and return value type.But it's also absolutely not supported at all, because OC allows you to define two functions with the same function name but different number of parameters, that is, OC supports function overloads with different number of parameters.

For example, we can define two functions in a class with different numbers of parameters, which are called differently by the number of parameters:

Overloaded function definition:

- (void)test:(int)one;
- (void)test:(int)one andTwo:(int)two;

Overloaded function implementation:


- (void)test:(int)one {
  NSLog(@"one parameter!");
}


- (void)test:(int)one andTwo:(int)two {
  NSLog(@"two parameters!");
}

Polymorphic calls:

[self test:1]; // output:one parameter!
[self test:1 andTwo:2]; // output:two parameter!

You can see that OC can overload a function by the number of parameters, but if the parameters are the same, neither the parameter nor the return value type can be compiled.The following definitions cannot be compiled from xcode:

- (void)test:(int)one;
- (int)test:(float)one; // Duplicate declaration of method 'test'

Thank you for reading, I hope to help you, thank you for your support on this site!


Related articles: