Java full reference manual (version 8th) Chapter 6th to Chapter 8th class method inheritance

Source: Internet
Author: User

1. New operator.

Dynamically (during runtime) allocates memory for the object and returns a reference to the object.

The object reference is similar to the memory pointer. The main difference is that the object reference cannot be referenced as the actual pointer operation, and the object reference cannot be directed to any memory location.

The basic types of Java are not implemented as objects, improving efficiency.

The advantage of memory allocation during runtime is that you can create any number of objects required. However, due to limited memory, you may not be able to allocate memory for objects due to insufficient memory, in this case, a running exception occurs.

Box b1 = new box (); box b2 = b1; then B1 and B2 reference the same object without allocating memory for B2 or copying any part of the original object. Create a referenced copy instead of a copy of the object.

2. This keyword.

This always references objects that call methods.

Local variables, including method parameters, and instance variables of ClassesDuplicate nameAt this time, the local variable hides the instance variable. This can solve this problem, for example, box (INT width,...) {This. width = width ;...}

3. Super keyword.

When explicitly initializing a super class member variable in the subclass constructor, not only does the code in the super class copy, resulting in low efficiency, but also implies that the subclass must be able to access these members. But sometimes you want to createOnly the superclass that knows the Implementation Details(Private member variables), sub-classes cannot directly access or initialize these variables. In this case, the super keyword is used.

Super generally hasTwo usage methods: It is used to call the superclass constructor. It is used to access the hidden member (that is, the member name) of the subcategory in the superclass ).

Super () must always be executed in the subclass ConstructorThe first statement. Super (Arg-list) // Arg-list is all the parameters required by the superclass constructor.

You can use a superclass to reference any object that inherits from a superclass. For example, class box {box (Box ob) {...} class EBox extends box {EBox (EBox ob) {super (OB );...}}.

When the member name of the subclass hides the member with the same name in the superclass, super. member can be used in the subclass to access the member variable in the superclass.

4. Finalize () method. "Termination" mechanism.

When an object is destroyed, you need to execute some actions. For example, if the object contains some non-Java resources, such as text handles or character fonts, ensure that these resources are released before the object is destroyed.

When a class object is to be recycled, The finalize () method is called during Java runtime. In the finalize () method, you can specify the actions that must be performed before the object is destroyed.

This method is not called when the object is out of its scope. This means that you do not know when the finalize () method will be executed, or even whether the finalize () method will be executed.

5. garbage collection.

The allocated memory is automatically released. Working principle: when an object is no longer referenced, it is deemed that the object is no longer needed and the memory occupied by the object can be recycled.

During the running of the program, only sporadic garbage collection will be performed. It is not simple because some objects no longer need to be recycled, different Java runtime implementations use different methods for garbage collection.

6. Overload methods.

The same class defines two or more methods that share the same name, as long as their parameter Declaration (type/quantity) is different. The return type is not limited.

Using objects as parameters is the most common method for calling constructors.

Parameter transferTwo methods are available: Value calling (copying the value of the real parameter to the form parameter of the Child routine without affecting the real parameter) and reference calling (passing the reference of the real parameter to the form parameter will change the real parameter ). Therefore, when an object is passed for a method, because the object is passed through a reference call, the form parameter and the real parameter reference the same object.

7. Rewrite method.

A subclass and a method of the superclass haveSame name and type signature. Only when both of them are at the same time will they be overwritten. Otherwise, they will only be overloaded.

If you want to access the override method in the superclass, you can use super to complete this operation.

Rewriting forms the basis for dynamic method scheduling. Dynamic Method scheduling is a mechanism through which the calling of rewrite methods can be parsed at runtime rather than during compilation. This is the mechanism of polymorphism during Java runtime.

A superclass reference variable can point to a subclass variable. Parse the call to the rewrite method at runtime. When calling the override method through a superclass reference, Java determines which version of the method to call based on the type of the referenced object during the call. This decision is made at runtime.

The purpose of the rewrite method is to interface multiple methods. By using the integration and Runtime polymorphism mechanisms at the same time, you can define multiple unified interfaces that are used for different but correlated object types.

8. Access Control.

Modifier public/private/protected. Protected is applied only when inheritance is involved.

9. Understand static.

Member variables and methods used by all objects of the class.

The Member is declared as static.You can access this member before creating any object of the class without any object references. It is essentially a global variable. When a class object is declared, a copy of static variables is not generated. On the contrary, all instances share the same static variables.

Method declared as static, Several restrictions: they can only directly call other static methods; can only directly access static data; can not reference this and super keywords in any way.

To initialize static variables, you can declareStatic code block. The static code block is executed only once. It is executed when the class is loaded for the first time. For example, static int A = 3; static int B; static {B = A * 4 ;}

Outside the class, these static members can be used without relying on any object. You only need to use the. Operator after the class name. For example, Class A {static void func () {...} Class B {public static
Void main (string ARGs []) {A. func (); // direct call }}

10. Introduction to final.

VariableDeclared as final, can prevent modifying the content of the variable, essentially a constant.

All final variable names are in upper case, encoding conventions.

Final variables must be initialized when declared. Two methods are used for initialization. One is to assign a value during declaration, and the other is to assign a value to the constructor.

Method parameters and local variablesIt can also be declared as final. Declare the parameter as final to avoid modifying the parameter in the method. Partial Variables declared as final can prevent multiple assignments.

When final is used for inheritance,

Use final keywordsBlock rewrite. Declaring a method as final can sometimes improve performance. the compiler can freely inline calls to these methods because it knows that these methods cannot be overwritten by the quilt class. When a small final method is called, the Java compiler can usually copy the bytecode of the child routine and inline it with the compilation code of the calling method to eliminate the extra burden required for method calling. Generally, Java dynamically analyzes the call to a method at runtime, which is called late binding. However, because the final method cannot be overwritten, the call to the final method can be parsed at compilation, it is called early binding. For example, Class
A {final void func () {...,} cannot override this method in the subclass.

Use final keywordsBlock inheritance. Use final before class declaration. If a class is declared as final, the class'sAll methods are declared as final. It is invalid to declare the class as abstract and final at the same time.

11. nested and internal classes.

Define another class, nested class, within the class.

The scope of the nested class is limited to the class that contains it. The nested class can access members of the class that contains it, including private members, but the containing class cannot access the members of The nested class.

There are two types of Nested classes: static and non-static. Static Nested classes are nested classes that apply static modifiers. They can only access non-static members of classes through objects, that is, they cannot directly reference non-static members of classes, so they are rarely used.

The most important type of Nested classes isInternal class. The internal class is a non-static nested class. It can access all variables and methods of the external class and can be directly referenced by them. the reference method is the same as that of other non-static members of the external class.

12. string class introduction.

Objects of the string type cannot be changed. Once a String object is created, its content cannot be changed. The reason is as follows: to change a string, you can always create a new string containing the modified content. Java defines stringbuffer, which allows you to modify the string. Common method: equals () length () charat (). The second part is detailed.

13. Variable Length Parameter varargs.

It is identified by three periods. For example, void func (Int... V) {} // v is implicitly declared as an array of the int [] type. V. length.

The variable length parameter must be a method.Final StatementAnd a methodOnly oneVariable length parameter.

Overload the varargs method. You can distinguish between different parameter types or add one or more common parameters. You can also use the non-varargs method to reload data.

Overloading may cause unexpected errors. For example, void func (int... v) {} void func (Boolean... v) {} when calling func (); an error will be reported, because both are legal and ambiguous; and void func (int... v) {} void func (int n, int... v) {} calls func (1). An error is returned even if it cannot be determined.

14. Inheritance.

Only one superclass can be specified for any subclass created.

Although the subclass contains all the members of the superclass, The subclassInaccessibleThe members declared as private in the superclass.

Each subclass inherits its own hierarchy.All superclassesInAll features.

In the class hierarchy, the constructor is called from the superclass to the subclass. Whether or not super () is used (). If super () is not usedDefault constructor or no-argument Constructor.

15. abstract class.

Declare the structure of known abstract content without providing the complete implementation of each method. Only define the general form shared by all sub-classes and fill in details for each sub-class.

The abstract type modifier requires that a specific method must be overwritten by the quilt class. Subclass responsibility.

Abstract type name (parameter-list); // No method body is provided

Any class that contains one or more abstract methods must be declared as abstract, that is, abstract must be added before the class. Abstract classes cannot be instantiated, that is, no objects exist. In addition, the abstract constructor cannot be declared, and the abstract static method cannot be declared. All subclasses of an abstract class either implement all abstract methods in the superclass or declare them as abstract.

Although you cannot create an abstract class object, you can create its reference variable. For example

Abstract class A {... // constructor and member variable abstract void func ();}

Class A1 extends a {A1 (){}
Void func (){...}}

Class A2 extends a {A2 () {}void func (){...}}

Class B {public static void main (string ARGs []) {

A1 a1 = new A1 (); a2 A2 a2 = new A2 (); A; // an object cannot be created.

A = A1; A. func (); // call the func implementation in A1.

A = a2; A. func (); // The implementation in A2. Only when variables are referenced by a superclass can the rewrite method be parsed at runtime.

}}

16. recursion.

For recursive versions of many routines, their execution speed is slower than that of iterative versions, because additional function calls are added. A large number of recursive calls to methods may cause stack overflow because parameters and local variables are stored in the stack, and copies of these variables are created each time a new call is made, therefore, the stack may be exhausted and an exception may occur.

When writing a recursive method, there must be an if statement somewhere to force the method to return without executing a recursive call.

17. Object Class.

All other classes are subclasses of objects. This means that reference variables of the object type can reference any other types of objects. Therefore, any array can be referenced.

In the defined method, class <?> Getclass () [get the class of the object at runtime] void Policy () [restore a thread waiting for execution on the called object] void policyall () [restore all threads waiting on the calling object] void wait () void wait (long milliseconds, int nanoseconds) wait for another thread to execute. It is declared as final and cannot be overwritten.

But other methods can be rewritten, such as object clone () [Create a new object that is exactly the same as the object to be copied] Boolean equals (Object object) [determine whether an object is equal to another object] void finalize () [called before recycling an object that is no longer in use] int hashcode () [return the hash value associated with the called object] string tostring () [returns a string describing the object]


On

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.