[OC learning-12] What is class inheritance? Why inherit? As well as the inheritance inside the attention, OC-12

Source: Internet
Author: User

[OC learning-12] What is class inheritance? Why inherit? As well as the inheritance inside the attention, OC-12

(1) how to implement class inheritance?

// The original inherited root class. For example, the class ASStudent inherits from the root class @ interface ASStudent: NSObject {}@ end // now inherits from the existing custom class, for example, ASStudent inherits from Student @ interface ASStudent: Student {} @ end

(2) why learning inheritance?

The reason is that there is less code. Something can be reused, that is, something in the parent class. In the Child class, you can directly inherit the parent class without writing code again.


(3) Precautions for inheritance

A: The instance variable of the subclass cannot have the same name as the parent class;

B: The objects initialized by the subclass can call their own functions, or they can call the functions in the parent class, because they are inherited;

C: The instance variable type of the parent class remains unchanged in the subclass. The subclass can only directly access the public and protected instance variables of the parent class. The parent class and the subclass can only be called directly with the public class;

D: The subclass method can overwrite the method of the parent class (if the method name, number of parameters, type, and return value type are the same), The subclass method is generally more detailed, the methods of the parent class are more abstract.

E: initialization rules under multi-level inheritance. One is to re-load init; the other is to write the specified Initialization Method in your class; the third is the self-class init method, which calls the specified Initialization Method of the class, and the fourth is the specified Initialization Method of the class that calls the specified Initialization Method of the parent class. Otherwise, recursive calls may occur;

F: the message search rule first searches for the class and then searches for the parent class. Note that although class B creates Class B objects, [B display] is used to call the display method, and the display method calls the parent class [super display] method, but the object is still the class object, so the value is the value of the class initialization.


[Eight Xi kiddies] Why should I use inheritance in java?

Inheritance is a cornerstone of object-oriented programming technology because it allows the creation of hierarchical classes. Using inheritance, you can create a general class that defines the general features of a series of related projects. This class can be inherited by more specific classes, and each specific class adds something unique to itself. In Java terminology, the inherited class is superclass, And the inherited class is subclass ). Therefore, a subclass is a special version of a superclass. It inherits all instance variables and methods defined by the superclass and adds unique elements to it.

To inherit a class, you only need to use the extends keyword to merge the definition of one class into another. To understand how to inherit, let's start with a short program. The following example creates A superclass A and A subclass named B. Note how to use the keyword extends to create A subclass of.

// A simple example of inheritance.
// Create a superclass.
Class {
Int I, j;
Void showij (){
System. out. println ("I and j:" + I + "" + j );
}
}
Class B extends {
Int k;
Void showk (){
System. out. println ("k:" + k );
}
Void sum (){
System. out. println ("I + j + k:" + (I + j + k ));
}
}
Class SimpleInheritance {
Public static void main (String args []) {
A superOb = new ();
B subOb = new B ();
System. out. println ("Contents of superOb :");
SuperOb. showij ();
System. out. println ();
SubOb. I = 7;
SubOb. j = 8;
SubOb. k = 9;
System. out. println ("Contents of subOb :");
SubOb. showij ();
SubOb. showk ();
System. out. println ();
System. out. println ("Sum of I, j and k in subOb :");
SubOb. sum ();
}
}

The output of this program is as follows:

Contents of superOb:
I and j: 10 20

Contents of subOb:
I and j: 7 8
K: 9

Sum of I, j and k in subOb:
I + j + k: 24

As you can see, subclass B includes all the members of its super Class. This is why subOb can obtain I and j and call the showij () method. Similarly, inside sum (), I and j can be directly referenced, just as they are part of B.

Although A is A super class of B, it is also A completely independent class. Being a subclass of A superclass does not mean that the superclass cannot be used by itself. In addition, a subclass can be a superclass of another class. The general form of declaring a class that inherits superclasses is as follows:

Class subclass-name extends superclass-n ...... remaining full text>

Q: What is the difference between class inheritance and interface inheritance in C?

A class is a single inheritance with multiple interfaces.
When different classes belong to the same type, they are inherited, but interfaces are used when they share the same performance. If a common implementation is required, they can only be inherited; otherwise, it is best to use interfaces, because once we inherit our base class, we cannot inherit other classes, and this problem is avoided by using interfaces.
String is the alias of String, and its usage is the same.

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.