6

I'm new to IOS , can anyone tell what is the difference between method overloading and overriding in IOS.I have gone through this but it is java ,i would like to know the same for IOS (objective c or swift,if possible with some code)

What is the difference between method overloading and overriding?

Community
  • 1
  • 1
Nesta K
  • 91
  • 1
  • 1
  • 2

4 Answers4

28
  1. Overloading happens at compile-time while Overriding happens at runtime: The binding of overloaded method call to its definition has happens at compile-time however binding of overridden method call to its definition happens at runtime.

  2. Static methods can be overloaded which means a class can have more than one static method of same name. Static methods cannot be overridden, even if you declare a same static method in child class it has nothing to do with the same method of parent class.

  3. The most basic difference is that overloading is being done in the same class while for overriding base and child classes are required. Overriding is all about giving a specific implementation to the inherited method of parent class.

  4. Static binding is being used for overloaded methods and dynamic binding is being used for overridden/overriding methods.

  5. Performance: Overloading gives better performance compared to overriding. The reason is that the binding of overridden methods is being done at runtime.

  6. private and final methods can be overloaded but they cannot be overridden. It means a class can have more than one private/final methods of same name but a child class cannot override the private/final methods of their base class.

  7. Return type of method does not matter in case of method overloading, it can be same or different. However in case of method overriding the overriding method can have more specific return type (refer this).

  8. Argument list should be different while doing method overloading. Argument list should be same in method Overriding.

  9. Overriding needs inheritance while overloading not.

Community
  • 1
  • 1
Harshal Wani
  • 2,249
  • 2
  • 26
  • 41
4

Method overloading in ios:

objective-C does not support method overloading, so you have to use different method names. Note, though, that the "method name" includes the method signature keywords (the parameter names that come before the ":"s), so the following are two different methods, even though they both begin "writeToFile":

-(void) writeToFile:(NSString *)path fromIntger:(int)anInt;
-(void) writeToFile:(NSString *)path fromString:(NSString *)aString;

The names of the two methods are "writeToFile:fromIntger:" and "writeToFile:fromString:".

Method overriding in ios:

Method overriding is a language feature in which a class can provide an implementation of a method that is already provided by one of its parent classes. The implementation in this class replaces (that is, overrides) the implementation in the parent class.

When you define a method with the same name as that of a parent class, that new method replaces the inherited definition. The new method must have the same return type and take the same number and type of parameters as the method you are overriding.

Here’s an example:

@interface MyClass : NSObject

- (int)myNumber;

@end

@implementation MyClass : NSObject

- (int)myNumber {
    return 1;
}

@end

@interface MySubclass : MyClass

- (int)myNumber;

@end

@implementation MySubclass

- (int)myNumber {    
    return 2;    
}

@end
Sulthan
  • 128,090
  • 22
  • 218
  • 270
princ___y
  • 1,089
  • 1
  • 9
  • 27
2

Overriding in Obj-C and Swift works the same way as in Java (in Swift even denoted by override keyword).

Obviously there is a lot of confusion regarding overloading. Let's start with Swift. In Swift, in general it works in the same way as in Java:

func myFunction(a: Int)
func myFunction(a: String)

The same method name, accepting different parameters. The compiler will decide on which method to call depending on the type of the parameter. However, when adding additional parameters:

func myFunction(a: Int, b: Int)

the method name actually changes from myFunction(_:) to myFunction(_:b:) so in the traditional sense, this shouldn't be called overloading but I belive Swift is using only the first part of the name (myFunction) in some cases to differentiate between methods so this actually could be overloading, too. I am a bit unsure about this case.

In Obj-C, there is no overloading.

We cannot declare the following in Obj-C:

@interface MyClass : NSObject

- (void)myMethod:(id)parameterA;
- (void)myMethod:(NSInteger)parameterA;

@end

and when changing the number of parameters, we also have to change the method name

@interface MyClass : NSObject

- (void)myMethod:(id)parameterA;
- (void)myMethod:(NSInteger)parameterA withAnotherParam:(id)parameterB;

@end

Note the method names are -myMethod: and -myMethod:withAnotherParam: respectively. When the method name is different, it's not overloading.

However, even in Obj-C we can use the plain old C functions and they can be overloaded:

void functionA(int a) {
   ...
}

void functionA(int a, int b) {
   ...
}
Sulthan
  • 128,090
  • 22
  • 218
  • 270
-1

Method overloading is a concept that exists in java or c++, but doesn't exist in objective-c. Method overriding is the same concept in all languages.

deadbeef
  • 5,409
  • 2
  • 17
  • 47
  • What do you mean it's a concept that doesn't exist in objective-c or swift? You can clearly implement func a(a:int) and func a(b:String) in a class. Is that not considered overloading? – Moriya Oct 27 '15 at 11:17
  • 4
    @Animal Obj-C differs between methods by name not by type. There is no overloading. In Swift you can definitely overload. – Sulthan Oct 27 '15 at 11:19
  • I'd say it's just a terminology thing in obj-c it's in essence overloading there as well. But i see what you mean – Moriya Oct 27 '15 at 11:21
  • My mistake, Sulthan is right. But you definitely can't overload in obj-c. I edited my answer. The bottom line is that the definition of what it is doesn't change depending on languages. – deadbeef Oct 27 '15 at 11:24