First, let's take a look at the concise definition of overloading, and overwriting (rewriting):
Method overload: If two methods have the same method name but the parameters are inconsistent, then one method is an overload of the other method.
Method override: If a method is defined in a subclass, its name, return type and parameter signature match the name, return type and parameter signature of a method in the parent class, then it can be said that the method of the subclass covers the parent Class methods
Let's focus on the coverage issue, 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());//The run result is people Student s=new Student(); System.out.println(s.getName());//The run result is student People pp=new Student(); System.out.println(pp.getName());//The run result is student }The above result shows that the getName method of the student class successfully overwrites the parent class's method
Let's take a look at the overlay of variables:
public class People { protected String name="people"; } public class Student extends People { protected String name="student"; } public static vo id main(String[] args) { People p=new People(); System.out .println(p.name);//The run result is people Student s=new Student(); System.out.println(s.name);//The run result is student People pp=new Student(); System.out .println(pp.name);//The run result is people }Through running the results, I found that the overlay of variables is actually different from the method.
In my own words: the coverage of variables can only be considered as half-baked coverage at most.
Otherwise, upconversion will not occur if data loss occurs
People pp=new Student(); System.out.println(pp.name);//The run result is people
In my personal experience: the coverage of variables is easy to make mistakes. It makes people feel like they are back to the inheritance of C++ [This is not the inheritance of C++ with virtual]
Finally, let's look at another 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());//The run result is people Student s=new Student(); System.out.println(s.getName : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : ());//The run result is student People pp=new Student(); System.out.println(pp.getName());//The run result is student }Obviously, such coverage is the more useful coverage for us, because in this way, we can achieve the purpose of abstracting concrete objects into general objects, and real polymorphism.
The above is just my personal opinion. If there are any wrong things, please point them out and discuss them.