12-overwrite

Overwrite

After the inheritance relationship between the subclass and the parent class, the subclass will actually inherit all the definitions in the parent class, but there may also be inappropriate scenes. If it is found that the design in the parent class is insufficient and the method or property name in the parent class needs to be retained, overwriting will occur.

Method override

When the subclass defines the same method name as the parent class, and the parameter type and number are exactly the same (exactly the same as the parent method), it is called method override.
The method to be called must be a method that has been overwritten by the subclass. If the method has not been overwritten, the method provided in the parent class will be called;
after the method overwrite of the subclass, if you want to continue to call the method in the parent class, then You must use "super.method()"
as long as you call the parent method in a subclass, you must add "super.method()" before the method

Method override limit

Using method overwriting can better expand the functions of the parent class, but it also has its own requirements for overwriting. The overridden method cannot have stricter access control permissions than its parent class;
permission relationship: public> default (Do not write)> private; (private authority is the smallest, if the parent class is default, then the subclass can only use public or default definitions)
Q: Please explain the difference between Override and Overlording?
A: Are the returned parameters the same during Overloading?

the difference overloading override
name Overload Overwrite
concept The method name is the same, the type and number of parameters are different The method name, parameter type and number, and return value are the same
Authority No permission restrictions Overridden methods cannot have stricter control permissions
range Occurs in a class Occurs in the inheritance relationship class

There is no restriction on the return type when performing method overloading, but a good habit should keep the return type consistent;

Attribute override

When the subclass defines a member with the same name as the parent class, it is called attribute coverage.
If the attributes are encapsulated according to the standard development at this time, the subclass actually has nothing to do with the private properties in the parent class at this time , even if the name is the same, it is just equivalent to the subclass defining a new property;

Q: Please explain the difference between super and this?
A:

  • The use of this in the program class means to find the required attributes and methods from this class. If this class does not exist, find the definition of the parent class. If super is used, it means that the parent class is directly searched without searching for the subclass;
  • Both this and super can call the construction method, but this() calls the construction of this class, and super() is constructed by the subclass calling the parent class. The two statements must be placed in the first line of the construction method, so they cannot appear at the same time ;
  • this can represent the current object;

final keyword

The final description in the program is the function of the finalizer. Using the final keyword in Java can achieve the following functions, defining classes that cannot be inherited and methods and constants that cannot be overridden;
when the subclass inherits the parent class, it is actually The method in the parent class can be overridden, but if you don’t want a method to be overridden, you can use final to define;
in some system development, you can use 1 to indicate that the switch is turned on, and 0 to indicate that the switch is off, but if it is now directly Operation 0 or 1, it will cause confusion, so I hope it can be used by name; The
important application technology in the final keyword: you can use it to define constants, and the content of constants cannot be modified once defined;
constants are often public definitions, In order to reflect the concept of sharing, we often use the concept of global constants and use public static final int key = 1;to define;
**Note: **When defining global constants, each letter must be capitalized;
in the method, final can also be used to define parameters, which means a The concept of constant;

Guess you like

Origin blog.csdn.net/MARVEL_3000/article/details/111381824