Method rewrite/overwrite (override)
Characteristics:
1 typically occurs between parent and child classes, subclasses override methods in the parent class, the return value type of the method, the method name, and the formal parameter list are the same as the parent class.
Subclass permission modifier >= permission modifier for parent class
2. Once the method of the parent class is overridden, the subclass creates the object and calls the subclass after the overridden method;
[Email protected]: rewritten annotations, labeling the current method to override the parent class method, must satisfy the rewrite rule;
Differences between method overloads and overrides:
1. Overloading occurs in the same class, mainly provides a method with the same name and different parameters; overrides occur between parent and child classes, mainly for the purpose of combining polymorphic use, to improve program extensibility and maintainability
2. Overloading, the method name is the same, the parameter list is different, and the return value type and the permission modifier are not related; Overrides, the method name, the argument list, the return value type to be the same as the parent class, the subclass permission modifier >= the parent class permission modifier.
Super keyword (analogy the This keyword)
Super is a reference to the immediate parent class, and you can use the Super keyword to invoke the properties of the parent class (super. Property name), method (super. Method name), construct method (Super (Property 1, Property 2 ...) . must be placed in the first row of the constructor).
Object class
Object is the superclass, base class, Root class of all classes, under the Java.lang package, without a class that uses the extends keyword, the default extends Object;
Common methods in the object class:
1.toString (); Outputs the incoming object as a string
2.equals (); Compare the 2 reference data types, compare the reference (address), need to compare the content, generally need to rewrite the Equals method;
3.wait ()/notify (): Multithreading related
4.getClass (): Returns the Object run-time class for this.
5.hashCode (): Returns the hash code value of the object
6.finalize (): This method is called by the object's garbage collector when the garbage collector determines that there are no more references to the object.
The order in which the program executes in the inheritance:
Parent class static code block > Subclass Static code block > Parent code block > Parent class construction Method > Subclass code block > Subclass Construction Method
New subclass, the constructor of the parent class is called first, the parent class object is created, and then the child class instance is created.
The difference between = = and equals
1.== when comparing basic data types, the values are compared
2. When comparing reference data types, = = is the same as equals, comparing the current object's reference (address)
3. To compare the contents of an object, you need to override the Equals method
Java Chapter Fifth: Object-oriented (OOP)