Java-Differentiated method overlay and variable overlay _java

Source: Internet
Author: User

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.

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.