Java method rewriting and coverage rules, and the difference between equals and ==

Method rewriting or method overriding (overriding)

The subclass
rewrites the method inherited from the parent class according to the requirements. When rewriting, you can use the super. method to retain the method of the parent class. The
construction method cannot be overridden.

Method rewriting rules

The method of the same name
argument list is the same
return type or the same value is a subclass
access to the parent class is not strict
static methods of the parent class does not cover the non-static methods subclasses, non-static methods of the parent class can be overridden by subclasses be static
sub A class can define a static method with the same name as the parent class, so that the static method of the parent class can be hidden in the subclass (Note: super cannot be used in the static method)
The private method of the parent class cannot be overridden by the subclass and
cannot be thrown more than the parent class method The exception

Insert picture description here
Insert picture description here

The equals() method of the Object class

Compare whether two objects are the same object, if yes, return true
operator ==
simple data type, compare values ​​directly. Such as 12
Reference type, compare whether the two are the same object
1) The equals() method of the Object class and
There is no difference
(2) When there are special requirements, such as when the attributes are the same as the same object, equals() needs to be rewritten.
(3) Java.lang.String rewrites the equals() method to change the judgment of the equals() method In order to judge its value

1.

<parent type> <reference variable name> = new <subtype>();
At this time, the method called by the parent class reference variable is the method of the subclass covering or inheriting the parent class, and the method of the non-parent class is
referenced by the parent class Variables cannot call methods specific to subclasses.

2.

<subtype> <reference variable name> = (<subtype> )<parent type reference variable>;
In the process of downcasting, if it is not converted to the real subtype type, a type conversion exception will occur.
When using instanceof , The type of the object must have a subordinate relationship with the class specified by the parameter behind instanceof

Guess you like

Origin blog.csdn.net/jokertiger/article/details/112850514