override=override=override
override is closely related to polymorphism! It can be said that the two are completely equivalent.
rewrite
Method rewriting (override / overwrite)
* 1. Rewriting: After the subclass inherits the parent class, it can overwrite the method with the same name and the same parameter in the parent class, that is, rewrite the method with the same name.
*
* 2. Application: After rewriting, when a subclass object is created and a method with the same name and parameters in the subclass is called through the subclass object, what is actually executed is that the subclass rewrites the method of the parent class.
*
* 3. Rewritten regulations:
* Method declaration: permission modifier return value type method name (parameter list) throws exception type {
* //method body
* }
* The convention is commonly known as: the method in the subclass is called the overridden method, and the method in the parent class is called the overridden method
* ① The method name and formal parameter list of the method overridden by the subclass are the same as the method name and formal parameter list of the overridden method of the parent class
* ② The permission modifier of the method overridden by the subclass is not less than the permission modifier of the overridden method of the parent class
* > Special case: subclasses cannot override methods declared as private permissions in the parent class
* ③ return value type:
* >The return value type of the overridden method of the parent class is void, and the return value type of the method overridden by the subclass can only be void
* >The return value type of the overridden method of the parent class is A type, and the return value type of the method overridden by the subclass can be A class or a subclass of A class
* >The return value type of the overridden method of the parent class is a basic data type (for example: double), then the return value type of the method overridden by the subclass must be the same basic data type (must also be double)
* ④ The exception type thrown by the method rewritten by the subclass is not greater than the exception type thrown by the overridden method of the parent class (specifically put it in exception handling)
* **********************************************************************
* The methods with the same name and parameters in the subclass and the parent class are either declared as non-static (consider rewriting), or both are declared as static (not rewriting).
polymorphism
Simply put, it is: assign the subclass to the reference of the parent class, and call the method of the parent class through the reference (the attribute will not take effect) , so that if the subclass overrides, then call the subclass.
The reference of the parent class can call the method overridden by the subclass , but cannot call the method unique to the subclass (compilation cannot pass, because the parent class does not have this method)
This is also easy to understand: I don't know who the reference points to when I compile. He may point to the parent class or any subclass, so I simply stipulate that he cannot call anything other than the existing properties and methods of the parent class.
That is, only the method of the parent class can be called, and if the method of the parent class is overridden, the overridden method will be called
Applicable only to methods, not to attributes, attributes will call the parent class regardless of whether they are overridden
That is to say, the particularity of multiple units is reflected in the method rewritten by the subclass, which is subject to the constraints of compilation.
You don’t need to pay attention to so many details during real development, just refer to it.
Are there overrides for properties but not methods?
The property of the same name of the subclass will indeed override that of the parent class, but it will not trigger polymorphism, and there will be no situation where the parent class looks for the subclass
import org.junit.Test;
public class test1 {
@Test
public void test1() {
B newB = new NewB();
System.out.println(newB.num1);
}
}
class B{
int num1=3;
void f(){
this.func();
System.out.println(this.getClass());
}
void func(){
System.out.println("i use b");
}
}
class NewB extends B{
int num1=5;
void func(){
System.out.println("i use NewB");
}
private void testSuper() {
super.f();
}
}
result:
3
You can see the reference of which class is used to call the attribute of the reference. For example, the reference of B is called here. Although the real object is newB, the result is num1 of B.
In the same way, if it is changed to test1(), it is changed to
NewB newB = new NewB();
System.out.println(newB.num1);
The result is 5, which is consistent with the above conclusion.
Rewrite the situation that may be encountered:
For example, B inherits A, and C inherits B. If B rewrites the function f, then the C instance will call B's f, and if C also rewrites f, then it will call C's f. Conclusion: when calling an instance , It is also easy to understand that rewriting takes the youngest subclass of the instance (that is, looking up to the first rewritten parent class) as the final rewrite
The difference between this, super, and default in the code block
By default, it first checks the local variables in its own code block, and then looks for the properties and methods of the class, and then looks for the properties and methods of the parent class (it is normal to find inherited things), and then looks for the variables of the upper code block (conforming to The search logic of the code block, but there is an additional process of finding the parent class)
super is also called the parent class, and only calls the methods and properties of the parent class . The meaning of existence is that the methods and properties of the parent class are not easy to call, so use super to indicate that I only call the things of the parent class, don't overwrite it for me.
This directly checks the properties and methods of the class, and then finds the properties and methods of the parent class. This is always the type when it is new.
import org.junit.Test;
public class test1 {
@Test
public void test1() {
NewB newB = new NewB();
newB.testSuper();
}
}
class B{
void f(){
this.func();
System.out.println(this.getClass());
}
void func(){
System.out.println("i use b");
}
}
class NewB extends B{
void func(){
System.out.println("i use NewB");
}
public void testSuper() {
System.out.println(super);
System.out.println(this);
// super.func();
// this.func();
}
}
result
NewB@1615099
NewB@1615099
You can see that both this and super point to the same object, which makes sense.
subclass and superclass this
The principle is
In NewB: NewB this = newB
In B: B this=newB
It's polymorphism! ! That is, the this of the subclass can only call the properties and methods declared by the parent class in the code block of the parent class, but if the method declared by the parent class is overridden, it will directly call the subclass, and the property declared by the parent class will be called if the subclass also has But it will not call the subclass, because the property does not trigger polymorphism.
prove
class a{
int num=3;
public static void main(String[] args){
new NewB().f();
}
void f(){
System.out.println(num);
}
}
class b{
int num=3;
void f(){
this.func();
System.out.println(this.getClass());
}
void func(){
System.out.println("i use b");
}
}
class NewB extends b{
int num1=5;
void func(){
System.out.println("i use NewB");
}
}
result
i use NewB
class NewB
Add parent class B, subclass NewB, NewB has 1 object newB
Is the this pointer in B equivalent to B this=newB
import org.junit.Test;
public class test1 {
@Test
public void test1() {
NewB newB = new NewB();
newB.testSuper();
}
}
class B{
int num=3;
void f(){
this.func();
System.out.println(this.getClass());
System.out.println(this.num);
}
void func(){
System.out.println("i use b");
}
}
class NewB extends B{
int num=5;
void func(){
System.out.println("i use NewB");
}
public void testSuper() {
super.f();
this.f();
}
}
result
i use NewB
class NewB
3
i use NewB
class NewB
3
It can be seen that it is in line with the guess