Java foundation fourteen (inherited)

inherit


Why use inheritance

Code demonstrates describe multiple things. Found that among more things have common attributes and behaviors. The same code can be extracted, extracted in a separate class, in order to make between classes can have relationships, this mechanism requires the use of inheritance in Java provides. Inherited need to use the keyword extends.

Definitions inherited
from the terms of the code: We describe several classes, I found these classes have the same members of the majority
so we can put out duplicate content extraction part, described by another class, the new class is the relationship between the parent, the other is a subclass of parent class and subclass inheritance extends

From the business logic terms: between the parent and child classes must be is a relationship, it is a same kind

Inherited characteristics

  • Between classes is a single inheritance
  • Between the interface and the interface is a multiple inheritance
  • Inheritance is transitive between the A-> B-> C

Although multiple inheritance feature allows subclasses have more than one parent class, but its disadvantages are also obvious, there are two main aspects:

  • If you have the same name in more than a subclass inherits the parent class's instance variables, subclass when referring to this variable will be ambiguous, can not determine which variables should use the parent class
  • If you have multiple parent classes in the same way a subclass inheritance, the subclass has not override this method, it will be ambiguous when the method is called, you can not determine which method should call the parent class

Since inherit transitive, then we will have a final inside Java parent class Object class, which is the ultimate parent of any class of produce which we inherited Java system, abnormal system, IO system, Socket system, a collection system.

Note that the definition of inherited
not only the acquisition of other classes inherit a function away.
Once private inheritance is not considered
routine skill development of small and medium
multi-level inheritance inheritance hierarchy appears, usually watch function in the parent class, understand the basic functions of the system, create sub-class object to use this system function.

Inheritance benefits

  1. Appears inherited improve the reusability of code, improve software development efficiency.
  2. It appears inheritance between classes so that had a relationship, provides premise polymorphism

Features child the parent class member variables

Learn the benefits of inheritance brings us improve the reusability of code. Produces the relationship between class and class inheritance allows object or objects. When inheritance occurs, resulting in a change it between those members of the class?

Member variables

  • If the member variable names appear different subclasses of the parent class, then there is no problem of access
  • Member variable of the parent class is non-proprietary, subclass can directly access
  • If the member variable in the parent class private, and subclasses are not directly accessible
  • The parent class has member variables (non-private) sub-class has the same name as a static variable, then visit the subclass static

When there is a member variable of the same name in the parent class sub, to access the parent class member variable in the subclass you must use the keyword super to complete.

and this super similar usage, this: representatives of this class object reference for distinguishing the caller. super: parent representative memory, points to a parent class (byte code) space, and reference is not the parent class. Child the parent class member variable of the same name, this is not development, as described in the parent class once completed properties, the subclass directly on it

Subclass member variables to find the order

Local -> subclass member variables (stack) -> subclass static variables (static area)
-> parent class member variables (non-static parent space) -> parent class static variables (static area)


Features member functions of the parent class child

Member function

  • When an object by calling the method in the program, will first have to find no corresponding method in a subclass
  • The method will be executed if the presence of a subclass subclass, the subclass if present will not perform the corresponding method in the superclass

Method special case member - covered
when exactly the same manner as the parent class subclasses occurs, the operation will be covered, also referred to override rewritten, rewrite or overwrite, when a subclass needs parent class function, and the functions of the subclasses body when their own unique content, it can be rewritten in the parent class, so that followed the functionality of the parent class, but also defines the type of unique content. Rewrite the meaning? Function declaration reservations parent class (function), a subclass of functionality improvements.

Rewrite the details need to pay attention

  1. Method subclass overrides the parent class method must ensure that the parent class greater than or equal rights permissions
  2. Static can only cover static, static or is covered, meaning that there is no static function rewrite!
  3. Little attention to the wording: must be exactly the same: the return type of the function parameter list function name to be the same

Summary: When a class is a class in another, it is possible through inheritance, to extend the functionality, if the parent class function content provided when desired subclass specific definition using rewritable


Child the parent class characteristics of static variables and functions

Static variables and member variables, and functions as a static member function.


Characteristics constructor sub parent class

When creating a subclass object, the parent class constructor will be preferentially executed, as a subclass of all of the first row have a default constructor implicit Super (); statement, call the constructor for this class with this (argument list) statement, call the constructor of the superclass by super (argument list), for super (parameter list) call, provided that the parent must have a corresponding constructor.

Said this keyword when he spoke this (...) must also be no conflict in the first sentence? No conflict, because this (...) will be transferred to other constructors, other constructors final transfer will not go back, only the first sentence is super (...)

Why subclass object initialization must access the parent class constructor in? Because subclass inherits the contents of the parent class, it is necessary to look at how the contents of the parent class is initialized when the object is created.

When a parent is not an empty argument constructor, the constructor of the subclass must have a parent constructor super statement specifies to be accessed in


final keyword

Having described some class, you do not want to be inherited, or in some part is a fixed function method, a subclass can override. But when these special subclass inherits class, wherein the method can be rewritten, in order to prevent this from happening, it is necessary to use a keyword final, final mean for the final, immutable.
final is a modifier that can be modified class, members of the class, as well as local variables.

  • final modification of the variable represents the address of data stored variables can not be changed, and must be defined when the initial value

  • final modification of this class can not be inherited, but can inherit from other classes

  • final modification function This function can not be overwritten, but not the parent class final modification function, can be added after the final subclass overrides

  • The final modification reference type variable, a reference indicating that the reference variable is not changed, instead of referring to the referenced object
    data may be varied or

    When to use? Look at the specific needs

Published 70 original articles · won praise 56 · views 1996

Guess you like

Origin blog.csdn.net/qq_43624033/article/details/103403038