What is inheritance? What are the advantages and disadvantages of inheritance?
Inheritance refers to the properties and methods of an object that directly use another object.
Inheritance needs to conform to the relationship: is-a (e.g. dogs are dogs, cats are cats)
How do I find the parent class?
Draw out the public parts of many subclasses to form a class, the parent class
Subclasses inherit the parent class, which has the properties and behavior of the parent class, as well as its own special properties and behavior. (That is, the sub-class function is more powerful)
Benefits of Inheritance:
1. Extract the duplicated Code
2. Establishing a relationship between classes and classes
Disadvantages:
High coupling resistance
Features: Only single inheritance is allowed in OC, because multiple inheritance will have a "fatal block"
Define a subclass:
In the. h file
The Super keyword, when overriding a method in a subclass, allows the caller to skip this layer and invoke a method in the parent class.
Role:
(1) Call one of the methods in the parent class directly
(2) Super is in the object method, then the object method of the parent class is called, and super is in the class method, then the class method of the parent class is called.
Usage Scenario: Subclasses want to preserve some behavior of the parent class when overriding the parent class method.
Use method [Super Method name] to invoke the method of the parent class.
Polymorphism is a different response to different objects responding to the same method. (like sleeping, different people sleep in different postures, this is a polymorphic)
Benefits of Polymorphism:
1. Allow the same message interface to be defined in multiple classes
2. You can define a common calling method to simplify the invocation
Polymorphism simply refers to the parent class pointer to the subclass
Objective-c Inheritance and polymorphism