First, let's look at a concise definition of overloading, and overriding (overriding):
Method Overload: If there are two methods whose method names are the same, but the arguments are inconsistent, it can be said that one method is an overload of another method.
Method overrides: If you define a method in a subclass whose name, return type, and parameter signature match exactly the name, return type, and parameter signature of a method in the parent class, you can say that the subclass method overrides the parent class method
Let's focus on covering issues, taking the following code as an example:
public class People {public
String GetName () {return
"people";
}
}
Public class Student extends people {public
String GetName () {return
"Student";
}
}
public static void Main (string[] args) {
people P=new people ();
System.out.println (P.getname ())//running result is people
Student s=new Student ();
System.out.println (S.getname ())//running result is Student
people pp=new Student ();
System.out.println (Pp.getname ())//running result is student
The results show that the GetName method of the student class successfully overrides the method of the parent class
Let's take a look at the override of the variable:
public class People {
protected String name= "people";
}
Public class Student extends people {
protected String name= "Student";
}
public static void Main (string[] args) {
people P=new people ();
SYSTEM.OUT.PRINTLN (p.name)//Running result is people
Student s=new Student ();
SYSTEM.OUT.PRINTLN (S.name)//running result is Student
people pp=new Student ();
SYSTEM.OUT.PRINTLN (pp.name)//Run result is people
}
By running the result I found that the coverage of the variable is actually different from the method.
In my own words: variable coverage can only be a half-baked coverage.
Otherwise, the upward transition and no data loss occurs
People pp=new Student ();
In my personal experience, the coverage of variables can easily make people wrong. It feels like it's back to the C + + inheritance [this is not referring to C + + with virtual inheritance]
Finally, let's look at a piece of code:
public class People {
protected String name= "people";
Public String GetName () {return
name;
}
}
Public class Student extends people {
protected String name= "Student";
Public String GetName () {return
name;
}
}
Main (string[] args) {
people P=new people ();
System.out.println (P.getname ())//running result is people
Student s=new Student ();
System.out.println (S.getname ())//running result is Student
people pp=new Student ();
System.out.println (Pp.getname ())//running result is student
Obviously, such coverage is more useful for us to cover, because it can be achieved: the object of abstract objects as a general object, real and polymorphism
The above is just my personal opinion, there are some wrong places welcome to point out to discuss.