Thinking In Java Summary of trivial knowledge points (2)

Source: Internet
Author: User
Tags export class

26. Access Permissions
Private-> default-> protected-> public corresponds to the class internal access permissions, package access permissions, subclass access permissions for different packages (the same package is the same as the previous level), and Global access permissions. The vast majority of attributes of a class use private modification. In addition, some methods are only used to assist in the implementation of other methods of the class. These "Tools" methods should also use private modification. If a class is mainly used as the parent class of other classes, the method containing the maximum number of classes may only want to be overwritten by its subclass, rather than directly called by the outside world, use protected. If you want to expose the methods that can be freely called to other classes, you should use the public modifier. The top-level classes are generally free to use for other classes. Therefore, a large number of top-level classes use the public modifier. The class constructor exposes the class to other classes by using the public modifier to create the class objects.

27. Understanding encapsulation (the other two are inheritance and polymorphism)
Encapsulation refers to hiding the object state information inside the object. External programs are not allowed to directly access the internal information of the object. Instead, the internal information is accessed through the methods provided by this class.

28. There are two reasons for controlling access permissions to members. The first is to prevent users from touching the parts they should not touch. These parts are necessary for internal operations of the class, but they are not part of the interface required by the client programmer. Therefore, specifying methods and domains as private is a service for client programmers. In this way, they can clearly see what is important to them and what they can ignore, which simplifies their understanding of classes. Second, this is also the most important reason. In order for the designer of the class library to change the internal working mode of the class, there is no need to worry that this will cause a major impact to the client programmer. Access control ensures that no client programmer is dependent on any part of the underlying implementation of a class.

29. You can create a main method for each class, which makes Unit Testing of each class easy. Even if a program contains multiple classes, only the main () method of the class called by the command line will be called.

30. Because the subclass object will call the parent class constructor during initialization, if there is no default base class constructor or you want to call a constructor with parameters, you must use the super keyword to explicitly compile the statements that call the base class constructor and configure the statements with an appropriate parameter list.

Class Art {Art (int a) {print ("Art constructor" + a);} Art () {print ("Art constructor ");}} class Drawing extends Art {Drawing (String aa) {super (1); print ("Drawing constructor" + aa);} Drawing () {print ("Drawing constructor") ;}} public class Cartoon extends Drawing {public Cartoon () {// super ("zpc "); // if this clause is absent, the default no-argument constructor is called at one layer. If the annotation is canceled, the specified parent class constructor print ("Cartoon constructor") is called ");} public static void main (String [] args) {Cartoon x = new Cartoon ();}}

31. Upward Transformation: it is safe to convert from a dedicated type to a more general type (convert the child type to the parent type, because the export class is a superset of the base class (the subclass inherits the members of the parent class ).

32. final keywords (See 45)
All final static types with a constant initial value, that is, the compile time are named in uppercase letters. A static and final domain only occupies a storage space that cannot be changed. Final makes the value constant for the basic type, and final keeps the reference constant for the object reference. Therefore, if a variable is declared as final, its value cannot be changed. However, once the reference is initialized and points to an object, it cannot be changed to another object, however, the value of the object itself can be changed.
The final class cannot be inherited. All methods in the final class are implicitly specified as final because they cannot be overwritten. Of course you can explicitly add the final keyword, but it doesn't make any sense.

33. null

34. Method overloading in a class: The Chinese name of a class is the same, and the parameter list is different. Other methods, such as the return value type and modifier, have nothing to do with method overloading.

35. If the member variables are not explicitly initialized, the default initialization will be executed during class loading or object creation, but the local variables (except the form parameters) must be explicitly initialized, otherwise, the variable cannot be used. Local variables are stored in the stack memory of the method in which they are located. You can use the constructor to change the default initialization of member variables.

36. Before the system executes the constructor's execution body, the system has created an object, but this object cannot be accessed by external programs. It can only be referenced by this in the constructor. After the execution of the constructor finishes, this object is returned as the constructor's return value. It is usually assigned to another reference type variable, so that external programs can access this object.

37. When sub-classes overwrite (override) parent classes, pay attention to the following rules:
Three identical: The method name is the same, the form parameter list is the same, and the return value type is the same (the return value type is not required for method overloading)
Small: the exception class thrown by the subclass should be smaller or equal to the parent class.
Major: the access permission of the subclass method should be greater or equal to that of the parent class method (the subclass cannot reduce the access permission of the parent class)
Both the method to be overwritten and the method to be overwritten are both Class methods or instance methods, but not class methods or instance methods.
Routine:
Public class WithFinal extends MyFinal {private final void test () {print ("WithFinal. test ");}/* The reason why the above method can" Overwrite "test () is only because the private modifier member is invisible to the subclass, which is not covered here, instead, it is a new method created in the subclass * // * public final void test2 () {print ("WithFinal. the final modifier cannot overwrite the */public void test3 () {print ("WithFinal. test3 ");} // here is method override. The modifier of the subclass must be greater than or equal to the parent class} class MyFinal {private final void test () {print (" MyFinal. test ");} public final void test2 () {print (" MyFinal. test2 ");} protected void test3 () {print (" MyFinal. test3 ");}}

38. During inheritance, the Child class does not obtain the constructor of the parent class. The subclass constructor always calls the parent class constructor or explicitly. Otherwise, the constructor is implicit (No parameter constructor is called), from the Object class constructor to the current class.

39. If there is no inheritance relationship between the two types, that is, it is not on the same inheritance branch of the inheritance tree, the Java compiler does not allow forced type conversion.

40. Three necessary conditions for Polymorphism: inheritance, overwriting, and parent class reference pointing to subclass objects. Polymorphism comes from two types of referenced variables in Java: The compile-time type (declared type) and the runtime type (determined by the object actually assigned to the variable ). If the types and runtime types are inconsistent during compilation, polymorphism occurs.

41. In the runtime environment, JVM uses the following binding rules to access the methods and attributes of referenced objects by referencing type variables:
The instance method is bound to the method of the actually referenced object. This is a dynamic binding, and the JVM dynamically determines the running of the program.
The static (static) method is bound to the declared method of the type. This is a static binding and is bound during compilation.
Member variables (including static variables and instance variables) are bound to the declared type member variables. This is static binding and is bound during compilation.

42. instanceof is not a keyword, but an operator
A instanceof B: determines whether the object referenced by variable A is an instance of Class B (or its subclass) to ensure that the forced type conversion will not fail.
Note: The compiling type of the first operand of the instanceof operator is either the same as that of the next class or the parent class of the back class. Otherwise, an error occurs during compilation.


43. design the principles that the parent class follows: try to hide the internal data of the parent class (set the parent class attribute to the private type ).
The auxiliary methods in the parent class are set to private. The methods in the parent class that need to be called by the external class are modified as public. If you do not want the subclass to override this method, use final. If you want a method in the parent class to be overwritten by the quilt class, but do not want to be accessed by other classes, use protected.
Note: Do not call the method that covers the quilt class in the parent class constructor as much as possible to avoid any problems.
// The following code causes a null pointer exception because the parent class constructor is executed first. The test method called by the parent class constructor is actually overwritten by the called subclass, at this time, // The subclass has not been initialized. If the name value is null, you can change the test method of the parent class to private. At this time, the method is not overwritten. In the parent class constructor, test () directly call the test method of the Base class public class Sub extends Base {String name = "zpc"; public void test () {System. out. println ("subclass override parent class method, name String Length" + name. length ();} public static void main (String [] args) {Sub s = new Sub () ;}} class Base {public Base () {test ();} public void test () {System. out. println ("Method for overwriting quilt classes ");}}
 
// The following example clearly shows the initialization sequence and can better understand the process of the above example: public class Sub extends Base {System. out. println ("Sub's normal initialization block");} static {System. out. println ("Sub static initialization block");} String name = "zpc"; public Sub () {System. out. println ("Sub () constructor");} public static void main (String [] args) {Sub s = new Sub (); System. out. println ("***************"); Sub s2 = new Sub () ;}} class Base {static {System. out. println ("Base Static initialization block");} {System. out. println ("Base's common initialization block");} public Base () {System. out. println ("Base () constructor"); test ();} public void test () {System. out. println ("Method for overwriting quilt classes ");}}

44. The following program demonstrates creating a singleton class (if a class can only create one instance at a time, this class is called Singleton class ):
Class Singleton {// use a variable to cache the private static Singleton instance of the created instance; // hide the constructor private Singleton () {}// provides a static method to return the public static Singleton getInstance () {if (instance = null) {instance = new Singleton ();} return instance ;}} public class TestSingleton {public static void main (String [] args) {Singleton s1 = Singleton. getInstance (); Singleton s2 = Singleton. getInstance (); System. out. println ("s1 = s2? "+ (S1 = s2); // s1 and s2 point to the same object }}
45. final Summary
(1) final variable
Unlike common member variables, final member variables (including class attributes and instance attributes) must be explicitly initialized by the programmer. The system does not implicitly initialize final members.
The final-modified class attributes and instance attributes can specify the initial values as follows:
Class Attribute: You can specify the initial value when initializing a block or declaring this attribute.
Instance attribute: You can specify an initial value in a non-static initialization block, declare the attribute, and constructor.
Public void fuzhi (final int d) {d = 9; // invalid. The Initialization is completed by the parameter passed in when the method is called. No value can be assigned}
(2) final Method
The final modifier method cannot be overwritten. For details about the routine, see (37). If you do not want the subclass to override a method of the parent class, you can use final to modify the method.
(3) final class
Final-modified classes cannot be inherited.

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.