Preface: Near the job hunting season on the Internet to see a Java interview clearance key points summary set think should be useful, but the author seems to have not sorted out the answer, so ready to write this serialization for their sorted out the answer and share, I hope to finish writing. Each update two questions, the answers from the network so not necessarily for reference only.
First, object-oriented characteristics
Object-oriented approaches have three basic characteristics: encapsulation, inheritance, and polymorphism. Abstraction is also an important part of object orientation, but note that it is not one of the characteristics of object orientation. The reason is that all programming languages require abstraction, and when the developer abstracts, he should consider which features are required by the software system, and then these features should be documented in the program. Therefore, there is no definite rule about which features need to be abstracted, but it depends on the functional requirements of the software system.
(1) Encapsulation (Encapsulation)
The so-called encapsulation is to encapsulate objective things into abstract classes, and classes can only allow their own data and methods to be operated by trusted classes or objects, to hide the information of the untrusted. Encapsulation is one of the characteristics of object orientation and is the main feature of object and class concepts. Simply put, a class is a logical entity that encapsulates data and the code that operates on that data. Inside an object, some code or some data can be private and inaccessible to the outside world. In this way, objects provide different levels of protection against internal data in the program
(2) the Inheritance (Inheritance)
Inheritance is the ability to take all the functionality of an existing class and extend it without having to rewrite the original class. New classes created by inheritance are called “subclasses” or “derived classes,” and inherited classes are called “base classes,” “parent classes,” or “superclasses.” The process of inheritance is the process of going from general to particular. Inheritance can be achieved by means of Inheritance and Composition. The concept of inheritance can be implemented in two ways: implementation inheritance and interface inheritance. Implementation inheritance is the ability to directly use the properties and methods of the base class without additional coding; Interface inheritance refers to the ability to use only the names of properties and methods, but subclasses must provide implementation; Unexpected changes or incorrect use of a private part of an object.
(3) Polymorphism (Polymorphism)
Polymorphism refers to different subclass objects that behave differently by passing them to them. Polymorphism means that the same method of a class instance has different manifestations in different situations. The polymorphism mechanism enables objects with different internal structures to share the same external interface. This means that although the specific operations on different objects are different, they (those operations) can be invoked in the same way through a common class.
(4) Non-characteristic: abstract
Ignore the things in a topic that are not relevant to the current goal and focus on the aspects that are relevant to the current goal. The abstraction is not intended to know all the problems, but to select some of them and leave out some of the details for the time being. There are two aspects of abstraction, a data abstraction, but a process abstraction.
- Data abstraction –> represents the characteristics of a class of things in the world, namely the properties of objects. For example, birds have wings, feathers, etc.
- Process abstraction –> represents the behavior of a class of things in the world, namely, the behavior of objects. For example, birds can fly and sing.
Reference links:
-
Java Foundation: Three characteristics and five principles of object orientation
-
Three characteristics and five principles of object – oriented
-
Three Characteristics of Java Object Orientation (in detail)
Ii. Differences between final, finally and Finalize
(1) the final
The final keyword can be used to modify variables, methods, and classes to indicate that the variables it modifies are immutable. Final modified member variables are immutable, if a member variable is the basic data type, initialize the members after the value of a variable cannot be changed, if a member variable is a reference type, so it can only point to the initialization time pointing to the object, is not able to point to other objects, but objects of content is allowed to change. The final keyword is used before a method declaration: this means that the method is the final method and can only be called, not overridden, but can be overridden. According to Crazy Java p122, return value types cannot be used in Java to distinguish method overloading. Overloading (two identical and different) : methods in the same class with the same name and different argument lists. Overrides: A subclass extends extends parent class. A subclass contains a method with the same name and parameter list as the parent class. A subclass method returns a value smaller or equal to that of its parent class; Subclass methods have greater access than parent methods. Classes that are final cannot be inherited (String, StringBuilder, StringBuffer, Math, immutable classes). None of the methods in these classes can be overridden, so you cannot use abstract and final at the same time. Abstract classes are intended to be inherited by subclasses, as opposed to final.
(2) to finalize
Finalize method comes from java.lang.Object and is used to recycle resources. You can add finalize methods to any class. The Finalize method will be called before the garbage collector cleans up the object. In practice, do not rely on using this method to reclaim any scarce resources, because it is difficult to know when this method is called. Garbage collection is only responsible for the recovery of memory, is not responsible for the resource recycling, recycling can be done by a programmer, the Java virtual machine will call first before recycling garbage objects finalize methods are used to make objects release resources (e.g., close the connection, close the file), after the garbage recycling, this method does not generally show a call, The garbage collector is actively invoked at garbage collection time.
(3) finally
When the code throws an exception, it terminates processing of the rest of the method and exits execution of the method. If we open a file and an exception occurs while processing the file, the file is not yet closed, then a resource reclamation problem will occur. Java provides a good solution to this problem in the finally clause. Statements in the finally clause must be executed, so we just put the file closure statement in the finally clause and the file closure statement will be executed regardless of the exit from the read or write file. The reasonable recovery of resources is guaranteed.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class Finally {
void fileWith(){
InputStream in = null;
try {
in = new FileInputStream("wang.txt"); } catch (FileNotFoundException e) {e.printstackTrace (); }finally{ try { in.close(); } catch (IOException e) { e.printStackTrace(); }}}}Copy the code
Reference links:
Differences between Final, Finalize, and finally in Java
Final, finally, Finalize difference
-over