Dark Horse programmer _java Basic Knowledge Essence Summary

Source: Internet
Author: User

This paper summarizes the basic knowledge in Java, including: 1, initialization of objects, 2, 2, inheritance, initialization of objects, 3, about construction method, 4, public, private and protected;5, abstract class, 6, final keyword, 7, Interface Interface;8, multiple inheritance, 9, interface embedding, 10, class embedding, 11, Exception class.

1. Initialization of objects

(1) Initialization of non-static objects
When an object is created, all data members of the class in which the object resides are initialized first.
Base type: type int, initialized to 0.
If object: These objects are initialized sequentially.
※ The constructor method of this class is called to create the object after all class member initialization is complete.
The function of the construction method is initialization.

(2) Initialization of static objects
The static variables of the main class in the program are initialized before the main method executes.
When you create an object for the first time, all static variables in the class are initialized, and when you first access a static object of a class (note that no such object is created at this time), all static variables are initialized in the order in which they are in the class.

2. When inheriting, the initialization process of the object

(1) The superclass of the main class is high to low, initializing static members sequentially, regardless of whether the static members are private.
(2) Initialization of the main class static member.
(3) The superclass of the main class is called from high to low for the default constructor method. Note that the initialization of non-static objects for this superclass is preceded by the invocation of the default constructor method for each superclass.
(4) Initialization of non-static members of the main class.
(5) Call the constructor method of the main class.

3, about the construction method

(1) A class can have no constructor method, but if there are multiple constructors, you should have a default constructor, or, if you inherit this class, you need to explicitly invoke one of the non-default constructor methods of the parent class in the subclass.
(2) In a constructor method, you can call only one other construction method, and the statement that calls the constructor method must be the first statement.

4. About public, private and protected

(1) A class that has no public modification, the condition that can be accessed by another class is: A. Two classes in the same file, B. Two classes in the same folder, and C. Two classes in the same package.
(2) Protected: Classes that inherit classes and the same package can be accessed.
(3) If the constructor method is private, you cannot create objects of that class in other classes.

5. Abstract class

(1) An abstract class cannot create an object.
(2) If a method in a class is an abstract method, the class must be an abstract class.
(3) A class inheriting an abstract class must implement an abstract method in an abstract class in a class.
(4) Abstract classes can have abstract methods, but also non-abstract methods. The abstract method cannot be private.
(5) A class that indirectly inherits an abstract class may not give the definition of an abstract method.

6. Final keyword

(1) An object is a constant that does not represent a member of an object that cannot be transformed and can still operate on its members.
(2) Constants must be assigned before they are used, but in addition to being initialized at the same time as declarations, they can only be initialized in the constructor method.
(3) The final modified method cannot be reset (a method with the same name cannot appear in the subclass).
(4) If a class is declared final, all methods are final, regardless of whether they are final decorated, but the data members can be final or not.

7, interface interface (with implements to implement the interface)

(1) All data in the interface is static and final. Although these two keyword modifiers can be used, the Chang must be given an initial value.
(2) The methods in the interface are public, in implementing the interface class, the implementation method must be the Public keyword.
(3) If you use public to decorate an interface, the interface must be the same as the file name.

8. Multiple inheritance

(1) A class inherits a class and interface, then the class must be written in front, the interface is written behind, and the interfaces are separated by commas.
(2) The interface can be multiple inheritance, pay attention to the use of keyword extends.
(3) Although a class only implements an interface, but not only to implement all the methods of this interface, but also to implement the interface inherited by the interfaces method, all the methods in the interface must be implemented in the class.

9, the interface embedded

(1) In an interface embedded class, you can use the private adornment. At this point, the interface can only be implemented in the class in which the other classes cannot access.
(2) The interface in the embedded interface must be public.

10. Embedding of Classes

(1) A class can be embedded in another class, but cannot be embedded in an interface.
(2) in Static or other methods, the inner class object cannot be created directly, and must be obtained by means of the method.

There are two kinds of means:

Class A {
Class B {}
B Getb () {
b b = new B ();
return b;
}
}

static void M () {
A = new A ();
a.b ab = A.getb (); or a.b ab = A.new B ();
}

(3) One class inherits the inner class of another class, because the superclass is an inner class, and the constructor method of the inner class cannot be called automatically, so it is necessary to explicitly call the constructor method of the superclass in the constructor method of the subclass. Next Example:

Class C extends A.b {
C () {
New A (). super (); This sentence implements a call to the inner class construction method.
}
}

The construction method can also be written like this:

C (a A) {
A.super ();
}//Use this construction method to create the object to be written as C C = new C (a); A is the object of a.

11. Exception class

In addition to the RuntimeException class in Java, other exceptions must be caught or thrown.

Dark Horse programmer _java Basic Knowledge Essence Summary

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.