Inheritance is one of the most prominent features of object orientation. Inheritance is the derivation of a new class from an existing class, which can absorb the data attributes and behavior of the existing class and extend the new capabilities.

Java is a single-inheritance language, and all classes have a common parent, the Object class. If a class does not explicitly extend from a class with extends, it inherits from the Object class by default.

Object class common methods:

  • Object()

Object constructor, there’s nothing to say about this. (Not important)

  • registerNatives()
  	private static native void registerNatives(a);  
	static {      
        registerNatives();  
    }
Copy the code

The Object class defines a static initialization block that is first called when Java objects are created. The registerNatives() method is called in the static initialization block above and is qualified with private, indicating that this method is private and cannot be called externally. We can infer from the method name that the method is registered with a local method. Which methods are registered? Explain in a post later) (Non-key)

  • clone()

The clone method is called to copy objects in the Java language. The so-called copy object first allocates a space of the same size as the source object, and creates a new object in the new space. Only a class implements the Cloneable interface can call clone () method, otherwise throw CloneNotSupportedException anomalies.

Clone is a protected method in the Object class. The subclass overrides the external call to make it public and implements the Cloneable interface

** Shallow copy: The **clone method is shallow copy, passing values to base data types and passing references to reference data types.

** Deep copy: ** Copies all attributes and the dynamic memory allocation to which the attributes point. There are two ways to implement deep copy. One is to clone the reference type within the object again. The second, more common way to implement deep copy is through serialization (such as Apache Commons-Lang3, which serializes using Json).

  • getClass()

Used to get the type of the runtime. This method returns the Object’s Class/runtime Class, which is the same as Object.class. (Note: there may be further questions about class loading mechanism, reflection, etc.)

  • equals()

The equals method is used to determine whether this and obj refer to the same block of memory. If this and obj refer to the same block of memory, the equals method is used to determine whether this and obj refer to the same block of memory. Returns true, or false if this and obj refer to different blocks of memory.

Note that the String class overrides equals, so you can use equals to determine whether strings are equal.

== and equals()

== is used to determine whether the addresses of two objects are equal. That is, to determine whether two objects are the same object. (Base data types compare values, reference data types compare memory addresses)

Equals is generally two cases. Equals is equivalent to == if the class does not override equals. If you override equals, compare objects to be equal by overriding them.

Override the equals principle:

  1. Reflexivity (self = self)
  2. Symmetry (y=x =y)
  3. Consistency (multiple calls with consistent results)
  4. Transitivity (A=B,B=C, A=C).

Non-null rule: t1.equals(null) returns false (if t1 is not null)

// Override the equals method's standard flow
public boolean equals(Object obj){
    // First step: Now check whether the two object addresses are equal
    if(this == obj)  return   true;
    // Return false if the argument is null;
    if(obj == null)  return   false;
    // Step 3: Return false if the two objects are not of the same type
    if(getClass() ! = obj.getClass())return false;
    / /?? if(! (this.getClass.getName().equals(o.getClass.getName())) return false;
    // Step 4: Force the object to be compared to the specified type, and then customize the comparison rules
    Student s = (Student) obj;
    if(s.name.equals(this.name)&&s.age==this.age) return true;
    else return false;
}
Copy the code
  • hashCode()

The value returned by this method is a hash code of type int, which is a value of type int calculated by the JDK based on the address of the object. The hash code of the object is designed to better support the hash mechanism of Java collection classes. Often overridden with equals to ensure that two objects that are equal have the same hashCode.

Need to pay attention to

  1. If two objects call equals() the same, then the hashCode returned by calling hashCode() must also be equal; Conversely, hashCode() of two objects wants to wait but equals() doesn’t necessarily want to wait.
  2. Overriding equals() requires overriding the hashCode() method.
  3. The hashCode() method is primarily used to increase hash table performance. For example, if a HashMap is an array linked list structure, the objects on the list can be directly compared by calculating their positions through the hashCode. Greatly improved performance
  • toString()

The toString() method returns a string representation of the object, usually just for output purposes, a method added specifically to facilitate string manipulation of all classes. The string content is the object’s type +@+ memory address value. Because the toString method returns a memory address, and you often need to get a string representation based on an object’s attributes in development, you also need to override it. Printing an object is simply printing the return value of the object’s toString method. We can override the class’s toString() method to print the data we need. Public String toString () {… }

  • wait()

Puts the current thread into the wait state. Until another thread calls notify() or notifyAll() on the object. After the wait() method is called, the lock on the object is released. (Note: this may refer to thread communication and the difference between Wait and sleep.)

  • wait(long timeout)

Puts the current thread into the wait state. Until another thread calls notify() or notifyAll() of this object, or the timeout timeout set by the parameter is exceeded. After the wait() method is called, the lock on the object is released.

  • wait(long timeout, int nanos)

Like the wait(long timeout) method, there is an additional nanos parameter, which represents the extra time (in nanoseconds, ranging from 0 to 999999). So we have to add nanos nanoseconds to the timeout.

  • notify()

Wake up a thread waiting on this object (note that this may extend to question thread communication)

  • notifyAll()

Wakes up all threads waiting on this object

  • finalize()

This method is used to trigger actions when instances are collected by the garbage collector. The object’s garbage collector calls this method when the GC (garbage collector) determines that there are no more references to the object.