Differences between overload and override

Source: Internet
Author: User

Differences between overload and override:

Java polymorphism Override:

Override is a type of polymorphism between child classes and parent classes.
Override allows subclass to change some behavior of the parent class.
Why Override is required: when the parent class does not meet some requirements of the Child class, we need the Child class to Override some behavior of the parent class.
For example, the phone numbers of employees in a company cannot be disclosed, while those of sales personnel (employees) must be disclosed.
Then we can do this:
Java code www.2cto.com
1. public class Employee {
2.
3. private String mobile;
4.
5. public Employee (String mobile ){
6. this. mobile = mobile;
7 .}
8.
9. protected String showMess (){
10. return "phone number:" + mobile;
11 .}
12 .}
Public class Employee {

Private String mobile;

Public Employee (String mobile ){
This. mobile = mobile;
}

Protected String showMess (){
Return "phone number:" + mobile;
}
}

The showMess method of the employee class is protected, so the objects in other packages cannot be accessed.

Then define a Sales personnel class and inherit the Employee class

Java code
1. public class Sales extends Employee {
2.
3. // In addition to attributes of the parent class, sub-classes can also have their own attributes.
4. private String msn;
5.
6. public Sales (String mobile, String msn ){
7. super (mobile );
8. this. msn = msn;
9 .}
10.
11. @ Override
12. public String showMess (){
13. return super. showMess () + "= msn:" + this. msn;
14 .}
15 .}
Public class Sales extends Employee {

// In addition to attributes of the parent class, the subclass can also have its own attributes.
Private String msn;

Public Sales (String mobile, String msn ){
Super (mobile );
This. msn = msn;
}

@ Override
Public String showMess (){
Return super. showMess () + "= msn:" + this. msn;
}
}

Note that the access level of the overwritten showMess method is public and can be accessed by any other object.

Note the following points about Override:
1. The access control level of the method to be overwritten can be different.
For example, in the previous example, the access level of the showMess method of the parent class is protected, and the access level of the showMess method covered by the subclass is public.

However, the subclass access level must be higher than the access level of the method covered by the parent class. It is incorrect if the parent class is public and the subclass is protected.

2. A method defined as private, static, or final cannot be overwritten.

3. Return type of the method: The return type of the subclass can be a more specific Object. For example, it is also correct to change the return type of the Employee class to the Object type. In turn, this is an error.

4. When a method is called, the override method is first found in the subclass. If the subclass does not exist, the method is found in the parent class.

Java code
1. public class Parent {
2.
3. private int num (int I, int j ){
4. return I + j;
5 .}
6.
7. public static void main (String [] args ){
8. Parent p = new Child ();
9. System. out. println (p. num (1, 2 ));
10 .}
11 .}
12. class Child extends Parent {
13.
14. public int num (int x, int y ){
15. return x-y;
16 .}
17 .}
Public class Parent {

Private int num (int I, int j ){
Return I + j;
}

Public static void main (String [] args ){
Parent p = new Child ();
System. out. println (p. num (1, 2 ));
}
}
Class Child extends Parent {

Public int num (int x, int y ){
Return x-y;
}
}

Why is the execution result of this code? If you answer-1, it is wrong. The correct answer is 3.
Why? Because the num method of the parent class is private, it cannot be overwritten. Therefore, the num method of the subclass is not an Override, therefore, if the subclass does not find the overwritten num method, the num method of the parent class will be executed. Therefore, the output result is 3.


Java code
1. public class Parent {
2.
3. public int test (){
4. // execute the num method of the subclass.
5. return num (1, 2 );
6 .}
7.
8. protected int num (int I, int j ){
9. return I + j;
10 .}
11.
12. public static void main (String [] args ){
13. Parent p = new Child ();
14. System. out. println (p. test ());
15 .}
16.
17 .}
18. class Child extends Parent {
19.
20. public int num (int x, int y ){
21. return x-y;
22 .}
23 .}
Public class Parent {

Public int test (){
// Execute the num method of the subclass
Return num (1, 2 );
}

Protected int num (int I, int j ){
Return I + j;
}

Public static void main (String [] args ){
Parent p = new Child ();
System. out. println (p. test ());
}

}
Class Child extends Parent {

Public int num (int x, int y ){
Return x-y;
}
}

The execution result of this Code is-1, because the test method of the parent class calls the num method of the subclass.


Java overload principle:
In java, overload follows two principles: accuracy and uniqueness.

Example 1:

Public class TestOverLoad
{
Public static void main (String [] args)
{
Test test = new Test ();
Test. print (null );
}
}

Class Test
{

Public void print (String some)
{
System. out. println ("String version print ");
}

Public void print (Object some)
{
System. out. println ("Object version print ");
}

}

In this program, the null value in test. print (null) is String and Object. What is the function to be executed? The result is that it executes public void print (String some), because the accuracy principle is that String inherits from Object. in java, null is more accurate than null is of the Object type.

Example 2:

Public class TestOverLoad
{
Public static void main (String [] args)
{
Test test = new Test ();
Test. print (null );
}
}

Class Test
{

Public void print (String some)
{
System. out. println ("String version print ");
}

Public void print (Object some)
{
System. out. println ("Object version print ");
}

Public void print (StringBuffer some)
{
System. out. println ("StringBuffer version print ");
}
}
In this example, the Test class has one more function, but an error occurs during compilation because this example violates the uniqueness principle of overload.

. There is no inheritance relationship between String and StringBuffer, so null cannot be said to belong to the class in both of them, so the program will not know the call to public void print (String some) during execution ), or call public void print (StringBuffer some ).

 

 

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.