Basic knowledge points that java needs to understand (1) Three major characteristics of object-oriented

The three major characteristics of object-oriented: encapsulation, inheritance, polymorphism

Encapsulation: abstract the abstract thing into an object, privatize the properties of the object, and provide some methods that can be accessed by the outside world;

Encapsulation is to surround the process and data, and the access to the data can only be through the defined interface. Object-oriented computing begins with the fundamental concept that the real world can be represented as a collection of fully autonomous, encapsulated objects that access other objects through a protected interface. Encapsulation is an information hiding technology, and encapsulation is realized through keywords private, protected and public in java. What is encapsulation? Encapsulation combines all the components of an object. Encapsulation defines how the program refers to the data of the object. Encapsulation actually uses methods to hide the data of the class and control the extent to which users can modify the class and access the data. Proper packaging can make the code easier to understand and maintain, and also enhance the security of the code.

​ Inheritance: subclasses extend new data domains or functions, and reuse the properties and functions of the parent class, single inheritance, multiple implementations;

Inheritance is one of the most notable features of object-oriented. Inheritance is to derive a new class from an existing class. The new class can absorb the data attributes and behaviors of the existing class, and can expand new capabilities.
Java inheritance is a technology that uses the definition of an existing class as the basis to create a new class. The definition of a new class can add new data or new functions, or use the functions of the parent class, but it cannot selectively inherit the parent class.
This technology makes it very easy to reuse previous codes, which can greatly shorten the development cycle and reduce development costs. For example, you can first define a class called car. The car has the following attributes: car body size, color, steering wheel, tires, and the car class derives two classes, car and truck. Add a small trunk for the car, and add a small trunk for the truck. Add a large cargo box.

​ Polymorphism: through inheritance (rewriting of the same method by multiple subclasses), or through interfaces (implementing interfaces and overriding interfaces)

Polymorphism is the ability to have multiple different manifestations or morphologies of the same behavior.
Polymorphism is the same interface, using different instances to perform different operations.
Three necessary conditions for the existence of polymorphism
1. Inheritance or implementation: In polymorphism, there must be subclasses and parent classes with inheritance or implementation relationships
2. Methods Rewriting: The subclass redefines some methods in the parent class (rewriting, rewriting using the @Override annotation)
3. The base class reference points to the derived class object, that is, the parent class reference points to the subclass object, and the parent class type : Refers to the parent class type inherited by the subclass object, or the parent interface type implemented

1. The difference between Java and C++

​ Differences: C++ supports multiple inheritance, and has the concept of pointers, and the memory is managed by the programmer himself; Java is single inheritance, and multiple inheritance can be implemented with interfaces. Java does not provide pointers to directly access memory, program memory is safer, and Java With JVM automatic memory management mechanism, programmers are not required to manually release useless memory

2. The principle of polymorphism

The underlying implementation of polymorphism is dynamic binding, which associates method calls with method implementations at runtime.

Static binding and dynamic binding:

One is determined at compile time and is called static dispatch, such as method overloading;

One is determined at runtime and is called dynamic dispatch, such as method overriding (overriding) and interface implementation.

polymorphic implementation

​ The stack frame of the current method call (local variable table, operation stack, dynamic connection, return address) will be stored in the virtual machine stack. The implementation process of polymorphism is the process of dynamic dispatch of method calls. If the subclass overrides the method of the parent class, then in the polymorphic call, the dynamic binding process will first determine that the actual type is a subclass, so that the subclass is searched first method in . This process is the essence of method overriding.

3. static and final keywords

**static:** can modify attributes and methods

​static modified attributes:

​ Class-level attributes, shared by all objects, are loaded with the loading of the class (only loaded once), before the creation of the object; it can be called directly by using the class name.

​static modification method:

​ Loaded with the loading of the class; you can use the class name to call directly; in the static method, only static members can be called, and this is not available;

**final:** keywords are mainly used in three places: variables, methods, and classes.

​final modifier variable:

​ If it is a variable of basic data type, its value cannot be changed once it is initialized;

​ If it is a variable of reference type, it cannot point to another object after it is initialized.

​final modification method:

​ Lock the method to prevent any inherited class from modifying its meaning (overriding); all private methods in the class are implicitly designated as final.

​final modification class:

When final modifies a class, it indicates that this class cannot be inherited. All member methods in a final class are implicitly designated as final.

A class cannot be inherited, except for the final keyword, there are constructors that can be private. (Inner classes are invalid)

4. Abstract classes and interfaces

**Abstract class: **A class containing abstract methods, that is, a class modified with abstract; abstract classes can only be inherited, so final modification cannot be used, and abstract classes cannot be instantiated.

**Interface: **Interface is an abstract type, which is a collection of abstract methods. The interface supports multiple inheritance. The methods defined in the interface are abstract methods modified by public abstract by default.

Same point:

① Neither abstract classes nor interfaces can be instantiated

② Both abstract classes and interfaces can define abstract methods, and subclasses/implementation classes must override these abstract methods

difference:

① Abstract classes have constructors, but interfaces have no constructors

③Abstract classes can contain ordinary methods, and interfaces can only be public abstract modified abstract methods (available after Java8)

③ Abstract classes can only be single-inherited, and interfaces can be multi-inherited

④ Abstract classes can define various types of member variables, and interfaces can only be static constants modified by public static final

Usage scenarios of abstract classes:

​ Not only want to constrain subclasses to have common behavior (but no matter how it is implemented), but also want to have default methods and instance variables

Application scenarios of the interface:

​ Constrain multiple implementation classes to have a unified behavior, but don't care how each implementation class is implemented; there may be no connection between the various functions in the implementation class

5. Generics and generic erasure

Generics:

The essence of generics is parameterized types. This parameter type can be used in the creation of classes, interfaces, and methods, known as generic classes, generic interfaces, and generic methods, respectively.

Generic erasure:

​ Java's generics are pseudo-generics. When using generics, type parameters are added, and they will be removed when the compiler compiles the generated bytecode. This process is called type erasure.

Types such as List will become List after compilation. What the JVM sees is just the List, and the type information attached by generics is invisible to the JVM.

Other types of elements can be added through reflection

6. Reflection principle and usage scenarios

Java reflection:

​ means that in the running state, for any class, all the properties and methods of this class can be known; and any method of it can be called;

Reflection principle:

​ Reflection is firstly able to obtain the bytecode of the reflection class in Java, and then map the methods, variables, constructors, etc. in the bytecode into corresponding methods such as Method, Filed, and Constructor

​How to get an instance of Class:

	1.类名.class(就是一份字节码)
	2.Class.forName(String className);根据一个类的全限定名来构建Class对象
	3.每一个对象多有getClass()方法:obj.getClass();返回对象的真实类型

scenes to be used:

  • Develop general frameworks - The most important use of reflection is to develop various general frameworks. Many frameworks (such as Spring) are configurable (such as configuring JavaBean, Filter, etc. through XML files). In order to ensure the versatility of the framework, it is necessary to dynamically load different objects or classes and call different methods according to the configuration file runtime.

  • Dynamic Proxy - In Aspect Programming (AOP), specific methods need to be intercepted. Usually, dynamic proxy is chosen. At this time, reflection technology is needed to realize it.

    JDK: spring default dynamic proxy, need to implement the interface

    CGLIB: serialize byte stream through asm framework, configurable, poor performance

  • Custom Annotation - Annotation itself is just a marker, it needs to use the reflection mechanism to call the annotation interpreter and execute the behavior according to the annotation mark.

7. Java exception system

Throwable is the superclass of all errors or exceptions in the Java language. The next layer is divided into Error and Exception

Error :

​ Refers to internal errors and resource exhaustion errors of the java runtime system. Applications do not throw objects of this class. If such an error occurs, in addition to notifying the user, the rest is to try to make the program terminate safely.

Exception 包含:RuntimeException 、CheckedException

Programming errors can be divided into three categories: syntax errors, logic errors, and runtime errors.

Syntax errors (also called compilation errors) are errors that occur during compilation, and are checked by the compiler to find syntax errors

A logic error means that the execution result of the program is not as expected, and the cause of the error can be located and found through debugging

Running errors are errors that cause abnormal terminals of the program, and running errors need to be handled through exception handling

RuntimeException: Runtime exception, the program should avoid such exceptions as much as possible from a logical point of view.

​ Such as NullPointerException, ClassCastException;

CheckedException: Checked exception, the program uses trycatch to catch and process

​ 如IOException、SQLException、NotFoundException;

Guess you like

Origin blog.csdn.net/luck_sheng/article/details/129033834