Java method coverage and variable coverage

Source: Internet
Author: User

First, let's take a look at the concise definition of overload and overwrite:

Method overload: If two methods have the same method name but different parameters, you can say that one method is the overload of the other method.

Method override: if a method is defined in a subclass, its name, return type, and parameter signature exactly match the name, return type, and parameter signature of a method in the parent class, you can say, the subclass method overwrites the parent class method.

We will focus on the coverage problem. The following code is used as an example:

 
 
  1. Public class People {
  2. Public String getName (){
  3. Return "people ";
  4. }
  5.  
  6. }
  7. Public class Student extends People {
  8. Public String getName (){
  9. Return "student ";
  10. }
  11. }
  12. Public static void main (String [] args ){
  13. People p = new People ();
  14. System. out. println (p. getName (); // The running result is "people ".
  15. Student s = new Student ();
  16. System. out. println (s. getName (); // The running result is student.
  17. People pp = new Student ();
  18. System. out. println (pp. getName (); // The running result is student.
  19.  
  20. }

The preceding result indicates that the getName method of the student class successfully overwrites the method of the parent class.

Let's take a look at the coverage of variables:

 
 
  1. Public class People {
  2. Protected String name = "people ";
  3.  
  4. }
  5. Public class Student extends People {
  6. Protected String name = "student ";
  7. }
  8. Public static void main (String [] args ){
  9. People p = new People ();
  10. System. out. println (p. name); // The running result is people.
  11. Student s = new Student ();
  12. System. out. println (s. name); // The running result is student.
  13. People pp = new Student ();
  14. System. out. println (pp. name); // The running result is "people ".
  15.  
  16. }

Through the running results, I found that the overwrite of variables is actually different from that of methods.

In my own words, the coverage of variables can only be considered half-hanging.

Otherwise, the upward conversion will not cause data loss.

 
 
  1. People pp = new Student ();
  2. System. out. println (pp. name); // The running result is "people ".

In my personal experience, it is easy for people to make mistakes by overwriting variables. It makes people feel that they have returned to the inheritance of C ++ [this does not mean the inheritance of C ++ with virtual]

Finally, let's take a look at the Code:

 
 
  1. public class People {  
  2.     protected String name="people";  
  3.     public String getName() {  
  4.         return name;  
  5.     }  
  6. }  
  7. public class Student extends People {  
  8.       
  9.     protected String name="student";  
  10.     public String getName() {  
  11.         return name;  
  12.     }  
  13. }  
 
 
  1. Main (String [] args ){
  2. People p = new People ();
  3. System. out. println (p. getName (); // The running result is "people ".
  4. Student s = new Student ();
  5. System. out. println (s. getName (); // The running result is student.
  6. People pp = new Student ();
  7. System. out. println (pp. getName (); // The running result is student.
  8.  
  9. }

Obviously, such coverage is more useful for us, because we can achieve the goal of abstracting a specific object into a general object, which is truly the same as polymorphism.

The above is my personal opinion. If you have any mistakes, please point them out for discussion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.