Java Basics for basics and two-dimensional arrays

Source: Internet
Author: User
Tags finally block modifier

One or two-D arrays

1. How to define
M: Represents how many one-dimensional arrays are in the current two-dimensional array; N: Represents the length of each one-dimensional array
(1) data type [] array name =new data type [M][n]
(2) data type [] array name =new data type [m][], given only m one-dimensional arrays, each one-dimensional array length is dynamically given.
(3) data type [] array name ={{element 1, Element 2, ...} ... {...}}, static initialization.
(4) data type [] array name []=new data type [M][n]
(5) data type array name [][]=new data type [M][n]
For example:

Operation Result:

Second, the code block

(1) Overview: Collectively, a block of code, enclosed in {}.
(2) Classification and characteristics:
A, local code block: in the main () method, limit the life cycle of the variable, external cannot access the variables in the local code block;
B, constructs the code block: In the Member method of a class, with {}, can put the same code block in several methods into the construction code block, initializes the object, executes the construction code block before each execution of the construction method;
C, Static code block: In the member position of a class, surrounded by {}, is static decorated, in general, used to initialize the class, and can only be executed once.
(3) Static code block, construct code block, construct method execution flow: Static code block > construct code block > Construct method.
For example:

Iii. inheritance

1. Overview: When multiple classes have the same properties or behaviors, they are extracted into a separate class, and multiple classes no longer need to define those properties or behaviors, just inherit that class. Where the inherited class is called a parent class, a base class, or a superclass, the inherited class is called a subclass or derived class.
2, Keyword: extends.
3. Subclass Inheritance Format:
Class subclass name extends parent class Name {... }
4. Design principle: cohesion and low coupling. Cohesion refers to the ability to implement a function (something that can be done with one class as far as possible), coupled with the relationship between classes and classes, minimizing coupling.
5. Characteristics of Inheritance:
(1) only supports single inheritance, does not support multiple inheritance (a class can have only one parent class, cannot have more than one parent class);
(2) Support multi-layer inheritance (inheritance system). For example:
Class a{}
Class B extends a{}
Class C extends b{}
(3) The subclass inherits the parent class, inherits all the members of the parent class (member variables, member methods, including private), but the subclass does not directly access the private members of the parent class, but only indirectly through the public access method of the parent class for the subclass to access.
6. Precautions:
(1) Subclasses can inherit all members of the parent class (member methods, member variables), but private members of the parent class, subclasses can only be accessed indirectly by creating public methods in the parent class, not directly;
(2) Subclasses cannot inherit the constructor of the parent class, but can access the parent class construction method through the Super keyword;
(3) Inheritance cannot be used for partial functions, and inheritance is used when the relationship between classes and classes embodies "is a".
(4) For example:

7. Super keyword
(1) Super represents the spatial identity of the parent class (that is, the reference to the parent class or the object of the parent class).
(2) Super is similar to this, the difference is as follows:
This: Represents a reference to this class.
Super: The identity that represents the storage space of the parent class (which can be understood as a parent class reference).
(3) Usage (this and super can be used as follows):
A, Access member variable: this. member variable name, super. Member Variable name
B, Access construction method: No parameter: This (), super (); ), super (... )
C, Access member methods: this. Member method (), Super. Member Method ()
8. Relationship of member variables in inheritance
Accessing a variable in a subclass method: (1) first in the sub-class local scope, (2) second in the subclass member scope to find; (3) Finally in the parent class member scope (absolutely inaccessible to the parent class local scope).
When a member variable defined in a subclass is consistent with the name of a member variable in the parent class, the member variable of the child class is accessed when the member variable is accessed using the method of the subclass.
9. Relationship of member methods in succession
(1) Subclasses access the parent class, which is called when the Member method name is inconsistent.
(2) When the member method name in the subclass is consistent with the name of the member method in the parent class: The member method in the subclass is searched first, if any, the member method in the parent class is found if none.
10. The relationship between the construction methods in succession
(1) All constructor methods in subclasses access the constructor of the parent's hollow parameter by default. Subclasses inherit data from the parent class and may also use data from the parent class, so it is important to initialize the parent data before the subclass initializes, and for this effect, a super () is defaulted on the first statement of the subclass construction method.
(2) If there is no construction method in the parent class, then: A, provide the parent class construction method, B, subclass through the Super keyword to show access to the parent class with the other constructor method, C, subclass through this call other construction methods of this class (there is a parameter construction), Indirect access to the parent class with a parameter construction method (the subclass must have a parameter-constructed method or a parameterless constructor method to initialize the parent class.)
(3) Note: Super and this are default in the first sentence of the construction method and must be in the first sentence, otherwise there will be multiple initialization of the parent data, can be omitted.
11. Initializing in Inheritance
(1) Initialization mode: hierarchical initialization.
(2) Initialization order: Subclass inherits Parent class, parent class is initialized first, subclass is initialized again.
(3) For example:

12. Advantages of inheritance
(1) Improve the reusability of the code (multiple classes with the same members placed in the same class);
(2) Improve the maintenance of the code (if the function code needs to be modified, one can be modified);
(3) The precondition of polymorphism, which makes the relationship between class and class.

Iv. polymorphic

1. Polymorphism Overview: Polymorphism refers to allowing objects of different classes to respond to the same message. That is, the same message can be used in a variety of different ways depending on the sending object (sending a message refers to a method call).
2. Multi-State premise
(1) must have an inheritance relationship;
(2) There must be a method override (method overlay);
(3) The application of the parent class must point to the subclass object (transition up): Parent class Name Object name =new subclass name ();.
3. Access Characteristics in polymorphic
(1) member variable: Compile look left, run look left;
(2) member method (non-static): Compile look left, run see right (existence method overrides);
(3) member method (static): Compile look left, run see left (Static and class related, not method rewrite);
(4) Construction Method: The constructor method, whether it is a subclass or a parent class, initializes the object.
4. Upward transformation/downward transformation
(1) Upward transformation: a reference to the parent class is directed to the child class object, and the parent class name parent class refers to the =new subclass name ().
(2) Downward transformation: The application of the subclass to the parent class reference, subclass name Subclass reference = (subclass name) The parent class reference, the downward transformation premise is must have the parent class reference, if uses improperly, can appear the runtime exception, namely ClassCastException.
(3) Examples:

5. Multi-State advantage
(1) Providing the reusability of the code, which is guaranteed by inheritance;
(2) Provide code extensibility, guaranteed by polymorphism.
6. Multiple State drawbacks
(1) Cons: Unable to access the unique features of subclasses.
(2) Solution: Using the downward transformation, the application of the child class to the parent class reference, subclass name Subclass reference = (subclass name) parent reference, downward transformation prerequisite is the parent class reference, if used improperly, there will be a run-time exception, that is, classcastexception.
7. Final keyword
(1) Overview: Final, final, end-state (cannot be modified).
(2) Function: Can be modified class, member method, variable.
(3) Features: A, can be modified class, but the class can not be inherited, so the declaration of the class can not exist with the abstract, B, can modify the member method, but the member method cannot be overridden, C, modifiable variable, at this time the variable is a constant, cannot be modified; d, the final decoration can only be assigned one time; Initialized and assigned when final is used; F, final can either modify the base data type, or modify the reference data type, if the basic data type is decorated, the value of the base data type cannot be changed, if the reference data type is used, the address value of the reference data type cannot be changed. However, the value of the member variable in heap memory can be changed.
(4) Final keyword interview questions:
Interview question: final, finally, finalize three difference?
Answer: A, final modifier (keyword), mainly used to modify the class, member methods, variables, and so on, the final state. The main role of a, can be decorated class, but the class can not be inherited, so the declaration of the class when the abstract can not be present and final, B, can be decorated member methods, but the member method can only be used cannot be overridden, C, modifiable variables, at this time the variable is a constant, cannot be modified and must give the initial value of the
B, finally, provides a finally block to perform any operation when the exception is handled. The finally block is executed regardless of whether an exception is thrown or captured. So in the code logic block, if there is code that must be executed regardless of what happens, put it in the finally block.
C, Finalize is the method name. Java technology allows the use of the Finalize () method to do the necessary cleanup before the garbage collector clears objects from memory. This method is called by the garbage collector to this object when it determines that the object is not referenced. The method is defined in the object class, so all classes inherit it. Subclasses override the Finalize () method to organize system resources or other cleanup work that is performed. The Finalize () method is called on the object before the object is deleted by the garbage collector.

V. Abstract class

1. Overview: If there is a method in a class that declares abstract functionality (abstract methods), the class is defined as an abstract class. If one of the classes has an abstract method, the class must be abstract, and abstract classes are not necessarily abstract methods.
2. Keywords: abstract
3. Subclass of Abstract class
(1) When a subclass is a concrete class, the subclass must implement the abstract functionality in the parent class.
(2) When a subclass is an abstract class, it is meaningless, cannot be instantiated, and cannot create an object.
4. Features: Abstract classes cannot be instantiated directly, that is, objects cannot be created.
5. Characteristics of abstract class members
(1) member variable: can be a constant, or it can be a variable.
(2) Member methods: There can be abstract methods or non-abstract methods.
(3) Construction method: can have no parameter construction method, can also have a parameter construction method, the role: to initialize the object.
6. Keywords that cannot coexist with abstract: Private, Final, static.
7, Example: abstract class:

Child Implementation class:

Test class:

Vi.. Interface

1, Overview: The interface is the extension of a thing xxx embodiment.
2. Keyword: interface
3. Format: Interface interface Name {//abstract function}
4, the sub-implementation of the interface class format: Class name +impl implements interface name {}
5, Characteristics: Can not be instantiated.
6, interface member characteristics
(1) member variable: is a constant, cannot be modified, and has default modifier public static final;
(2) Construction method: The interface does not exist the construction method;
(3) member method: cannot have method body, are abstract method, have default modifier: Public abstract.
7, interface, class relationship
(1) The relationship between classes and classes: inheritance, only supports single inheritance, does not support multiple inheritance, can be multi-layered inheritance.
(2) class-To-interface relationships: Implement relationships, one class inherits another, and multiple interfaces can be implemented.
(3) interface and interface relationship: is an inheritance relationship, can be either single-inheritance, or multiple inheritance.
8. The difference between an interface and an abstract class
(1) Member differences
Member variables:
Abstract class: Can be either constant or variable.
Interface: can only be constant, default modifier: public static final
Construction Method:
Abstract class: There are construction methods, no parameters/arguments (initialized to the object).
Interface: no construction method.
Member Methods:
Abstract classes: There can be abstract methods, or there can be non-abstract methods.
Interface: Only abstract method, default modifier: Public abstract
(2) The difference between class and Class/Class and interface/interface and interface
The relationship between a class and a class: an inheritance relationship that supports only single inheritance, does not support multiple inheritance, and can be inherited in multiple layers.
Class-To-interface relationships: Implement relationships, while one class inherits another, and multiple interfaces can be implemented.
Interface and interface: is an inheritance relationship, can be either single-inheritance, or multiple inheritance.
Interface 1 extends interface 2, interface 3 ... Interface N
(3) Differences in design principles
Abstract class: An inheritance relationship that embodies a relationship of "is a" (A is a type of B).
Interfaces: Classes and interfaces that implement relationships, embodying a "like a" relationship (extended functionality).
9, the actual development of the use of frequency
(1) Interface polymorphism: most used.
(2) Abstract class polymorphism: more.
(3) Creation of specific objects: often used.
10. Example
Abstract class:

Child Realization Class Students:

Sub-implementation class Teacher:

Interface + Test:

Vii. Internal Classes

1, Overview: In the Class B internal definition of Class A, then class A is the inner class of Class B.
2. Internal classes Access external classes: Inner classes can directly access members of external classes, including private members.
3. External class access Inner class: Indirect access by creating an inner class object.
Access format: External class name. Internal class Name Object name = External Class object. Inner class object.
For example:

4. Classification
(1) member Inner class: the member position of the outer class. Members of external classes can be accessed directly, including private members.
member Inner class modifier: A, Private: (function) to ensure the security of the data. B, Static: As an external class member, it is characterized by static internal classes accessing external class data, which must be decorated with static.
For example:

(2) Local inner class: The local position of the outer class. Members of the external class can be accessed, including private. When accessing a member method of an inner class in the local location of an external class, you need to create an inner class object in the local location, accessed through the object.
For example:

(3) Anonymous inner class
A, the premise: there is a class (the class can be a concrete class can also be an abstract class) or interface.
B, Format: New class name or interface name {method override ();}
C, Essence: Inherits the class or implements the interface subclass object.
D, anonymous internal category interview questions: complete code, output on the console ("Hello World"). As follows:

Viii. formal parameters and return values

1, overview: Formal parameters are the basic type, no effect on the actual parameters; formal parameters are reference types (classes, abstract classes, interfaces), which affect the actual parameters, as in the case below. When the return value is the base type, the data is received with the corresponding base type, and if the return value is a reference type, the case is as follows.
2, formal parameters for the specific class: the need to provide such objects.
Return value: The Class object.
For example:

3, the formal parameters for the interface: you need to provide the sub-implementation of the interface class, through the sub-implementation class instantiation (interface polymorphism).
For example:

4. Formal parameters are abstract classes: you need to provide subclasses of the abstract class to create objects in an abstract, polymorphic form.
Return value: The subclass object that requires the current abstract class (abstract polymorphic).
For example:

Ix. difference between overload and override

(1) Overload: Method overloads, like method names, have different parameters (number or type), regardless of the return value.
(2) Override: Method Override, subclass defines a method declaration that is the same as the parent class, used in inheritance.

Java Basics for basics and two-dimensional arrays

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.