Method rewriting, overloading, and hiding

Source: Internet
Author: User

First, rewriting and hiding occur in two classes, while overloading can occur in one class.

The concept of rewriting is as its name implies: re-write it. The method name, parameters, and returned values are identical, and the possible implementation process is different. Why rewrite it? Because the original method is insufficient for perfect or strong, or does not meet the developer's actual application requirements.

If multiple methods have the same name, but the parameters of these methods are different, the number of parameters is different, or the parameter type is different. This is called method overload.
Note: The return type cannot be used to distinguish between overloaded methods. Only two methods with the same name with different types are returned, which is easy to understand: if you write methods with identical parameters but different return types, the compiler cannot decide which method to call when calling.

Hide
The following is an interview question from Huawei.

Public class classa {
Public void methodone (int I ){}
Public void methodtwo (int I ){}
Public static void methodthree (int I ){}
Public static void methodfour (int I ){}
}

Public class classb extends classa {
Public static void methodone (int I ){}
Public void methodtwo (int I ){}
Public void methodthree (int I ){}
Public static void methodfour (int I ){}
}

1. Which methods hide the parent class?
2. Which methods overwrite the parent class?

The rewriting in the above question is obvious, that is, methodtow. the errors of methodone and methodthree are obvious. Subclass inherits the parent class. Two identical methods are static and one is not. How do you instantiate the compiler! The rest is hidden: static methods cannot be overwritten; that is to say, it is useless to write the methodfour subclass, or call the methodfour method of the parent class, therefore, the methodfour method hides the method of the parent class.
Sample:
Public class father {
Public static void overwritting (){
System. Out. Print ("Father method ");
}
}

Public class son extends father {
Public static void overwritting (){
System. Out. Print ("Son method ");
}
Public static void main (string ARGs []) {
Father son = new son ();
Son. overwritting ();
}
}
The running result of the above program is output: Father Method

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.