Dark Horse programmer--oc language inheritance and polymorphism

Source: Internet
Author: User

A) The basic concept of inheritance

Program of the world and the Human "object" of the world in the mind is no difference, rich two generations inherit the parents, naturally have all the resources parents have, the subclass inherits the parent class also has the parent class all methods and attributes (member variables).

Here animals are the parents of cats and dogs, and black and white cats are subcategories of cat classes.

The benefits of the "inheritance":

(1) The duplicated code is extracted.

(2) establishing a link between classes and classes

The disadvantages of inheriting:

Coupling is too strong

II) inheritance format

@interface Animal:nsobject

The animal inherits the NSObject, obtains the NSObject class the method;


@interface Dog:animal

Dog class inherits Animal class


Note: The OC language is a single-inheritance language. In the OC language, basically the root class of all classes is the NSObject class.

III) Succession considerations

1) The compiler executes from top to bottom, so there should be at least a declaration of the parent class in front of the subclass;

(2) The name of a member variable with the same name is not allowed in OC and the parent class;

(3) The subclass in OC can have the same name as the parent class method, in the subclass call, take precedence of their own internal search, if not the first layer of the upward looking;

Tip: The subclass overrides a method in the parent class, overwriting the previous implementation of the parent class.

Create a Student *s=[[student alloc] init];

The student class and the parent class of this class are loaded into memory at this point.

Tip: There is a super class pointer in each class that points to your parent class. There is an Isa pointer in the object that points to the class that called the object.

IV) succession and combination

Application of Inheritance:

(1) When two classes have the same properties and methods, the same properties and methods can be extracted into a parent class.

(2) When Class A fully possesses some of the properties and methods in Class B, it may be considered that Class B inherits Class A (consider), in which case a combination can also be considered.

Inheritance: # # #是xxx, such as dogs are animals, can let dogs inherit animal class

Combination: # # #拥有xxx, if the student has a book, you can make the book this class as a property of the student class

V) Super

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.


(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.

VI) polymorphic Concepts

Polymorphism in the code embodiment, that is, a variety of forms, must have inheritance, no inheritance is not polymorphic.

When polymorphic is used, dynamic detection is performed to invoke the real object method.

Polymorphism in code is manifested in that the parent pointer points to the subclass object, such as nsobject *obj = [[Dog alloc] init];

VII) Polymorphic Benefits

such as dog and Cat are animal subclasses, to feed dogs can be written

void feed (dog *d) {}, this time the function can only feed the dog. To feed a cat, you rewrite a feed function with the cat type parameter, and if you write

void Feed (Animal *a) {}, this time the function can feed all animals, including cats and dogs, which can greatly simplify the code.

VIII) Polymorphic Limitations

Polymorphism limitations: Pointer variables of a parent class type cannot directly invoke methods that are specific to subclasses.


Animal *a=[[dog alloc] init];

[A run];//does not have a run method in the animal class

Workaround: You can cast a to a variable of type dog*, as follows:

Dog *d= (dog *) a;//uses casts, where a and D point to the same dog object

Dark Horse programmer--oc language inheritance and polymorphism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.