Learn more about overloading and overriding methods in Java

Source: Internet
Author: User

First, we'll talk about the overloads of the method:

  The overload of Java is that you can create multiple methods in a class that have the same name but have different parameters.

Determine if overloading has only two conditions:

1) The same method name

2) The different parameters are as follows:

A, the method parameter type is different

B, the method parameter number is different

C, method parameters are arranged in different order

The code is as follows:

 PackageCom.tocean.test;//Package Name Public classTest1 { Public voidNameint i) {System.out.println ("Overloading"); }            //method types are different     Public voidname (String name) {System.out.println ("Overloading"); }           //method parameter number is different     Public voidNameint i,string name,double c) {System.out.println ("Overloading"); }    //parameters are arranged in different order     Public voidNameDouble C,int I, String name) {System.out.println ("Overloading"); }}  

Independent of method return value and permission modifier

The code is as follows:

1  PackageCom.tocean.test;//Package Name2  Public classTest1 {3     //independent of method return value4      Public intNameinti,string name) {        5         returni; 6     }       7     //independent of permission modifiers8     Private voidNameDoublec) {9System.out.println ("Overloading"); Ten     } One}

In addition is the rewriting of the method:

Java's overriding premise is inheritance, inheriting subclasses do not want to inherit the parent class's methods intact, but want to make some modifications, which requires a method of rewriting.

  There are five criteria for determining whether to rewrite:

A, the same method name

B: The same method parameters

C: Permission modifier cannot be narrower than parent class

D: Throwing exceptions cannot be more than parent class

E: The return value is consistent with the return value of the parent class or its subclasses

The code is as follows:

 Package com.tocean.test;  Public class Father {    publicvoid  Getsay (String str) {        System.out.println ("The parent class is saying:" + str);}    } class extends father{    // access modifier cannot be narrower than parent class, not method override    privatevoid Getsay (String str) {        System.out.println ("subclass in said:" +str);}    }

 Packagecom.tocean.test;Importjava.io.IOException; Public classFather { Public voidGetsay (String str)throws Exception{System.out.println ("The parent class is saying," +str); }}classSonextendsfather{//the method thrown is more than the parent class, not a method override (note the number of exceptions, not the range of exceptions)    Private voidGetsay (String str)throws ioexception,runtimeexception{System.out.println ("The subclass is saying:" +str); }}
 Packagecom.tocean.test; Public classFather { Public string getsay (String str) {System.out.println ("The parent class is saying," +str); returnstr; }}classSonextendsfather{//The return value of the method is the same as the parent class, which is the override of the method     Public string getsay (String str) {System.out.println ("The subclass is saying:" +str); returnstr; }}
 Packagecom.tocean.test; Public classFather { Public Object getsay (String str) {System.out.println ("The parent class is saying," +str); returnstr; }}classSonextendsfather{//The return value of the method is not the same as the parent class, but its subclass is the method's override     Public string getsay (String str) {System.out.println ("The subclass is saying:" +str); returnstr; }}

Learn more about overloading and overriding methods in Java

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.