Distinguish between method coverage and variable coverage in Java

  • 2020-04-01 04:10:45
  • OfStack

First, let's look at a concise definition of overloading and overwriting:

Method overloading: if two methods have the same method name, but different arguments, then one method is an overloading of the other.

Method override: if you define a method in a subclass whose name, return type, and parameter signature exactly match the name, return type, and parameter signature of a method in a parent class, you can say that the method in the subclass overrides the method in the parent class

Let's focus on coverage, with 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 result is people
 
  Student s=new Student(); 
 System.out.println(s.getName());//Run the result as student
 
 People pp=new Student(); 
 System.out.println(pp.getName());//Run the result as student
 
 } 

The above results show that the getName method of the student class successfully overrides the method of the parent class

Let's look at the coverage of variables:


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);//The result is people
   
  Student s=new Student(); 
  System.out.println(s.name);//Run the result as student
   
  People pp=new Student(); 
  System.out.println(pp.name);//The result is people
 
 } 

After running the results, I found that the coverage of variables is actually different from that of methods.

In my own words, variable coverage is at best a half-baked coverage.

Otherwise, the upward conversion and will not occur data loss phenomenon


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

In my personal experience, overwriting variables is a recipe for error. It feels like a return to C++ 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());//The result is people
   
  Student s=new Student(); 
  System.out.println(s.getName());//Run the result as student
   
  People pp=new Student(); 
  System.out.println(pp.getName());//Run the result as student
 
 } 

Obviously, such coverage is more useful for us, because it achieves the goal of abstracting concrete objects into general objects, which is exactly the same as polymorphism

The above is just my personal opinion, any wrong place is welcome to point out to discuss.


Related articles: