Basic java knowledge

Source: Internet
Author: User
Tags define abstract

[Java]
1. object initialization
(1) Non-static object initialization
When an object is created, all data members of the class where the object is located are initialized first.
Basic Type: int type, initialized to 0.
For objects: these objects are initialized in order.
※After all class members are initialized, the constructor of this class is called to create an object.
The role of the constructor is initialization.
(2) static object initialization
Static variables of the main class in the program are initialized before the main method is executed.
Not only is the first time an object is created, all static variables in the class are initialized, and the first time a static object is accessed (note that this class object is not created at this time, all static variables must also be initialized in the class order.
2. object initialization during inheritance
(1) initialize static members in sequence from high to low in the super class of the main class, regardless of whether the static members are private or not.
(2) initialize static members of the primary class.
(3) calls the default constructor from high to low for the superclass of the main class. Note: Before calling the default constructor of each superclass, initialize the non-static object.
(4) initialization of non-static members of the primary class.
(5) Call the constructor of the main class.
3. Constructor
(1) the class can have no constructor, but if there are multiple constructor methods, there should be a default constructor. Otherwise, when inheriting this class, you need to explicitly call a non-default constructor of the parent class in the subclass.
(2) In a constructor, only one other constructor can be called at a time. The statement that calls the constructor must be the first statement.
4. public, private, and protected
(1) If a class without public modification can be accessed by other classes, the condition is:. the two classes are in the same file. the two classes are in the same folder. the two classes are in the same software package.
(2) protected: the inherited class and the class of the same software package can be accessed.
(3) If the constructor is private, you cannot create objects of this class in other classes.
5. abstract class
(1) abstract classes cannot create objects.
(2) If a method in a class is an abstract method, the class must be an abstract class.
(3) classes that inherit abstract classes must implement abstract methods in abstract classes.
(4) abstract classes can have abstract methods or non-abstract methods. The abstract method cannot be private.
(5) classes that inherit abstract classes indirectly may not define abstract methods.
6. final keywords
(1) An object is a constant. It does not mean that the member of the object cannot be changed, and its members can still be operated.
(2) constants must be assigned a value before they are used, but they can only be initialized in the constructor unless they are declared at the same time.
(3) The final modifier method cannot be reset (a method with the same name cannot appear in the subclass ).
(4) If a class is declared as final, all the methods are final, no matter whether it is modified by final, but the data member can be final.
7. interface (use implements to implement the interface)
(1) All data in the interface is static and final, that is, static constants. Although these two keywords are not needed, the initial value must be assigned to the constant.
(2) All methods in the interface are public. In the implementation interface class, the implementation method must be public keywords.
(3) If you use public to modify an interface, the interface must be the same as the file name.
8. Multi-Inheritance ‑
(1) If a class inherits a class and an interface, the class must be written first, and the interface must be written later. Interfaces must be separated by commas.
(2) multiple inheritance is allowed between interfaces. Use the keyword extends.
(3) Although a class only implements one interface, it must not only implement all methods of this interface, but also implement methods of interfaces inherited by this interface, all methods in the interface must be implemented in the class.
9. Interface embedding
(1) The interface can be embedded into the class and can be modified using private. At this time, the interface can only be implemented in the class where it is located, and other classes cannot be accessed.
(2) the interface in the embedded interface must be public.
10. Class embedding
(1) A class can be embedded in another class, but cannot be embedded in an interface.
}
(2) In static or other methods, internal class objects cannot be directly created and must be obtained by Means.
There are two methods:
Class A {class B {} BgetB () {B B = new B (); return B ;}} static void m () {A a = new A ();. B AB =. getB (); // or. B AB =. new B ();}
(3) One Class inherits the internal class of another class. Because the superclass is an internal class, the constructor of the internal class cannot be automatically called, in this way, the super class constructor needs to be explicitly called in the constructor of the subclass. Example:
Class C extends A. B {C () {new A (). super (); // calls the constructor of the internal class. }}
The constructor can also be written as follows:
C (A a) {a. super () ;}// use this constructor to create an object, which must be written as C c = newC (a); a is the object of.
11. Except for the RunTimeException class, all exceptions must be caught or thrown.

1. object initialization
(1) Non-static object initialization
When an object is created, all data members of the class where the object is located are initialized first.
Basic Type: int type, initialized to 0.
For objects: these objects are initialized in order.
※After all class members are initialized, the constructor of this class is called to create an object.
The role of the constructor is initialization.
(2) static object initialization
Static variables of the main class in the program are initialized before the main method is executed.
Not only is the first time an object is created, all static variables in the class are initialized, and the first time a static object is accessed (note that this class object is not created at this time, all static variables must also be initialized in the class order.
2. object initialization during inheritance
(1) initialize static members in sequence from high to low in the super class of the main class, regardless of whether the static members are private or not.
(2) initialize static members of the primary class.
(3) calls the default constructor from high to low for the superclass of the main class. Note: Before calling the default constructor of each superclass, initialize the non-static object.
(4) initialization of non-static members of the primary class.
(5) Call the constructor of the main class.
3. Constructor
(1) the class can have no constructor, but if there are multiple constructor methods, there should be a default constructor. Otherwise, when inheriting this class, you need to explicitly call a non-default constructor of the parent class in the subclass.
(2) In a constructor, only one other constructor can be called at a time. The statement that calls the constructor must be the first statement.
4. public, private, and protected
(1) If a class without public modification can be accessed by other classes, the condition is:. the two classes are in the same file. the two classes are in the same folder. the two classes are in the same software package.
(2) protected: the inherited class and the class of the same software package can be accessed.
(3) If the constructor is private, you cannot create objects of this class in other classes.
5. abstract class
(1) abstract classes cannot create objects.
(2) If a method in a class is an abstract method, the class must be an abstract class.
(3) classes that inherit abstract classes must implement abstract methods in abstract classes.
(4) abstract classes can have abstract methods or non-abstract methods. The abstract method cannot be private.
(5) classes that inherit abstract classes indirectly may not define abstract methods.
6. final keywords
(1) An object is a constant. It does not mean that the member of the object cannot be changed, and its members can still be operated.
(2) constants must be assigned a value before they are used, but they can only be initialized in the constructor unless they are declared at the same time.
(3) The final modifier method cannot be reset (a method with the same name cannot appear in the subclass ).
(4) If a class is declared as final, all the methods are final, no matter whether it is modified by final, but the data member can be final.
7. interface (use implements to implement the interface)
(1) All data in the interface is static and final, that is, static constants. Although these two keywords are not needed, the initial value must be assigned to the constant.
(2) All methods in the interface are public. In the implementation interface class, the implementation method must be public keywords.
(3) If you use public to modify an interface, the interface must be the same as the file name.
8. Multi-Inheritance ‑
(1) If a class inherits a class and an interface, the class must be written first, and the interface must be written later. Interfaces must be separated by commas.
(2) multiple inheritance is allowed between interfaces. Use the keyword extends.
(3) Although a class only implements one interface, it must not only implement all methods of this interface, but also implement methods of interfaces inherited by this interface, all methods in the interface must be implemented in the class.
9. Interface embedding
(1) The interface can be embedded into the class and can be modified using private. At this time, the interface can only be implemented in the class where it is located, and other classes cannot be accessed.
(2) the interface in the embedded interface must be public.
10. Class embedding
(1) A class can be embedded in another class, but cannot be embedded in an interface.
}
(2) In static or other methods, internal class objects cannot be directly created and must be obtained by Means.
There are two methods: www.2cto.com
Class A {class B {} BgetB () {B B = new B (); return B ;}} static void m () {A a = new A ();. B AB =. getB (); // or. B AB =. new B ();}
(3) One Class inherits the internal class of another class. Because the superclass is an internal class, the constructor of the internal class cannot be automatically called, in this way, the super class constructor needs to be explicitly called in the constructor of the subclass. Example:
Class C extends A. B {C () {new A (). super (); // calls the constructor of the internal class. }}
The constructor can also be written as follows:
C (A a) {a. super () ;}// use this constructor to create an object, which must be written as C c = newC (a); a is the object of.
11. Except for the RunTimeException class, all exceptions must be caught or thrown.
 

 


Author: a125138

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.