Objective-c Inheritance and polymorphism

Source: Internet
Author: User

    • Inherited

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.

    • Polymorphic:

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

Related Article

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.