Abstract classes and interfaces
- An abstract class
- Cannot create an instance of the Abstrac class
- You can create variables of an abstract class type that point to an instance of a concrete subclass
- You can’t have abstract constructors and abstract static methods
- A subclass implements all the abstract methods of an abstract class
- Abstract classes can have non-abstract methods
- Abstract cannot modify the same class as final.
- Abstract cannot be private, static, final or native
- interface
- Variants of abstract classes
- All methods in an interface are not abstract, and methods decorated with default are not abstract
- You can only define static final member variables. By default, you do not write static final variables.
- The unimplemented methods of the implementation class override interface do not need to be overridden if the method is decorated with default
- 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
- 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.
- The Overloaded method is the type of the returned value that can be changed.
- Override to rewrite
- 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.
- 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
- A subclass cannot have lower access modifier rights than its parent class
Final and finalize
- final
- Declare properties: Properties are immutable
- Declare methods: Methods cannot be overridden
- Declare classes: Classes are not inheritable
- finalize
- A method of the Object class that is called on the reclaimed Object when the garbage collector executes
- 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
- Static inner class: Belongs to an external class and is loaded only once. The scope is only within the package and can pass
External class name. Internal class name
Direct 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 classes
Static inner class ThreadLoaclMap defines the inner class Entry. - 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.
- 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.
- 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
- Superclass static code blocks and static variables.
- Subclass static code blocks and static variables.
- Superclass ordinary code block and ordinary variable.
- Parent class constructor.
- Subclasses plain code blocks and plain variables.
- Subclass constructor.
List, Map, Set
- A List accesses elements with a specific index, and can have duplicate elements that inherit from the Collection interface
- Sets cannot hold duplicate elements (they are distinguished by their equals() method), inheriting the Collection interface
- 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.
- Both sets and maps are implemented in two versions based on hash storage and sorting trees
A HashMap and Hashtable
- Both HashMap and Hashtable implement the Map interface
- A HashMap allows keys and values to be null, whereas a Hashtable does not.
- Hashtable is synchronous, HashMap is not. Therefore, HashMap is better suited for single-threaded environments, while Hashtable is better suited for multi-threaded environments.
- 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.