Abstract classes and interfaces

  • An abstract class
  1. Cannot create an instance of the Abstrac class
  2. You can create variables of an abstract class type that point to an instance of a concrete subclass
  3. You can’t have abstract constructors and abstract static methods
  4. A subclass implements all the abstract methods of an abstract class
  5. Abstract classes can have non-abstract methods
  6. Abstract cannot modify the same class as final.
  7. Abstract cannot be private, static, final or native
  • interface
  1. Variants of abstract classes
  2. All methods in an interface are not abstract, and methods decorated with default are not abstract
  3. You can only define static final member variables. By default, you do not write static final variables.
  4. The unimplemented methods of the implementation class override interface do not need to be overridden if the method is decorated with default
  5. The instanceof operator can be used to determine whether an object’s class implements an interface

Overload and Override

Overloading and overwriting are different manifestations of Java polymorphism

  • Overload overloaded
  1. If multiple methods with the same name are defined in a class with different numbers of arguments or different parameter types, they are called Overloading.
  2. The Overloaded method is the type of the returned value that can be changed.
  • Override to rewrite
  1. If we define a method in a subclass that has the same name and parameters as its parent, we say that method can be Overriding.
  2. When you override the method, the original method is overwritten, and to use the original method, you need to call the super keyword to reference it
  3. A subclass cannot have lower access modifier rights than its parent class

Final and finalize

  • final
  1. Declare properties: Properties are immutable
  2. Declare methods: Methods cannot be overridden
  3. Declare classes: Classes are not inheritable
  • finalize
  1. A method of the Object class that is called on the reclaimed Object when the garbage collector executes
  2. You can override this method to provide other resource recycling at garbage collection time, such as closing files.

Object-oriented characteristics

  • abstract
Abstraction is the omission of those aspects of a topic that are not relevant to the current goal in order to give fuller attention to those aspects that are relevant to the current goal. Abstractions are not intended to know all of the problems, but to select some of them, leaving out some of the details for the time being. Abstract includes two aspects, one is process abstraction, the other is data abstraction.Copy the code
  • inheritance
Inheritance is a hierarchical model that binds classes together, allows and encourages reuse of classes, and provides a way to explicitly express commonalities. A new class of an object can be derived from an existing class, a process called class inheritance. The new class inherits the features of the original class. The new class is called a derived class (subclass) of the original class, and the original class is called the base class (parent class) of the new class. A derived class can inherit methods and instance variables from its base class, and the class can modify or add new methods to make them better suited to special needs.Copy the code
  • encapsulation
Encapsulation is the enclosing of processes and data that can be accessed only through defined interfaces. Object-oriented computing began with the basic concept that the real world could be represented as a series of fully autonomous, encapsulated objects that accessed other objects through a protected interface.Copy the code
  • polymorphism
Polymorphism refers to allowing objects of different classes to respond to the same message. Polymorphism includes parametric polymorphism and inclusion polymorphism. Polymorphic language has the advantages of flexibility, abstraction, behavior sharing and code sharing, which solves the problem of application function having the same name.Copy the code

Methods in Object

The inner class

  1. Static inner class: Belongs to an external class and is loaded only once. The scope is only within the package and can passExternal class name. Internal class nameDirect access, class access onlyAll static properties and methods of the external class. The Node Node for HashMap, the Sync class for ReentrantLock, and the SubList class for ArrayList are all static inner classes.Inner classes can also be defined in inner classesStatic inner class ThreadLoaclMap defines the inner class Entry.
  2. Member inner class: Each object belonging to an external class is loaded with the object.You cannot define static members and methods; you can access all the content of an external class.
  3. Local inner class:You cannot declare access modifiers. You can only define instance member variables and instance methods. The scope is only in the code block that declares the class.
  4. Anonymous inner classes: Nameless classes used only once to simplify code,The object type created is equivalent to the subclass type of new's class. Used to implement event listening and other callbacks.

The order in which subclasses are initialized

  1. Superclass static code blocks and static variables.
  2. Subclass static code blocks and static variables.
  3. Superclass ordinary code block and ordinary variable.
  4. Parent class constructor.
  5. Subclasses plain code blocks and plain variables.
  6. Subclass constructor.

List, Map, Set

  1. A List accesses elements with a specific index, and can have duplicate elements that inherit from the Collection interface
  2. Sets cannot hold duplicate elements (they are distinguished by their equals() method), inheriting the Collection interface
  3. Map stores key-value pair mappings. The mappings can be one-to-one or many-to-one. Only one null key/value pair is allowed. If there are multiple NULL -v key/value pairs, the last one is reserved.

  1. Both sets and maps are implemented in two versions based on hash storage and sorting trees

A HashMap and Hashtable

  1. Both HashMap and Hashtable implement the Map interface
  2. A HashMap allows keys and values to be null, whereas a Hashtable does not.
  3. Hashtable is synchronous, HashMap is not. Therefore, HashMap is better suited for single-threaded environments, while Hashtable is better suited for multi-threaded environments.
  4. A HashMap provides a set of keys to which iterations can be applied, so a HashMap fails quickly. Hashtable, on the other hand, provides Enumeration of keys.