This is the seventh day of my participation in the August More text Challenge. For details, see: August More Text Challenge

What is polymorphism

Polymorphism is generally associated with inheritance, the essence of which is that a subclass overrides or overrides the methods of its parent class so that a call to the same method on an object of the same class produces different results. An object of the same class refers to an object at a level above the inheritance hierarchy, which is more general.

Polymorphism in a program: a pointer to a parent class points to a subclass object


The principle of polymorphism (dynamic binding)

  • Dynamic typing allows a program to determine the true type of an object until execution
  • Dynamic type binding allows a program to determine which method to call on that object until execution time

Polymorphism condition

  • There is inheritance
  • A subclass overrides a superclass method
  • A parent pointer points to a subclass object

Performance in a program

When a superclass pointer points to an object of a different subclass, and the overridden method is called through the superclass pointer, that object’s method is executed


Application scenarios

  • Improved code extensibility

  • Advantages of polymorphic

    The main benefit of polymorphism is that it simplifies the programming interface. It allows the reuse of customary names between classes, rather than giving a new name to each new function that is added. In this way, the programming interface is an abstract set of behavior that distinguishes it from the class that implements the interface

    Polymorphism also allows code to be spread across different objects without trying to consider all possible objects in a single function. This makes the code particularly extensible and reusable. When a new scenario emerges, it simply adds a new class and a new method with the same name

  • Pay attention to the point

    If a superclass pointer points to a subclass object, a subclass-specific method must be cast to the subclass before it can be called

    • If polymorphism is present, the superclass has access to methods specific to the subclass
    • If there is no polymorphism, the parent class cannot access methods specific to the subclass

Example:

@interface Phone : NSObject
@property (nonatomic,strong) NSString *name;
- (void)call;
@end

#import "Phone.h"
@implementation Phone
- (void)call{
    NSLog(@"%s",__func__);
}
- (NSString *)description
{
    return [NSString stringWithFormat:@"% @", _name];
}
@end

---------------
#import "Phone.h"
@interface IPhone : Phone
@end

#import "IPhone.h"
@implementation IPhone
- (void)call{
    NSLog(@"%s",__func__);
}
@end
---------------
#import "Phone.h"
@interface Android : Phone
@end

#import "Android.h"
@implementation Android
- (void)call{
    NSLog(@"%s",__func__);
}
@end
---------------
- (void)viewDidLoad {
    [super viewDidLoad];
    Phone *p = [[IPhone alloc]init];
    p.name = @"iPhone";
    [p call];
    NSLog(@"% @",p);

    Phone *a = [[Android alloc]init];
    a.name = @"Android";
    [a call];
    NSLog(@"% @",a);
}
Copy the code

log: