Java overloads and overrides

Source: Internet
Author: User

Overloaded overload and override override are two representations of polymorphism.

Overloading overload, which occurs in the same class , is equivalent to a different method, just because the function is similar so that the same name, must contain different parameters , You can use a different return value type . such as code:

1  Public classOverloadtest {2     3      Public voidloadTest () {4System.out.println ("LoadTest First");5     }6     7      Public voidloadTest (String p_string) {8System.out.println ("LoadTest second");9     }Ten      One      Public intLoadTest (intp_time) { ASystem.out.println ("LoadTest Third"); -         returnP_time + 100; -     } the      -      PublicString LoadTest (intp_time, String p_str) { -System.out.println ("LoadTest Fourth"); -         returnP_str; +     } -  +}

Overwrite override, which occurs in subclasses

To create a parent class first overridetest

1  Public class overridetest {2     3      Public void test1 () {4         System.out.println ("Method of the base class"); 5     }67 }

Then create a subclass to inherit the parent class, you can overwrite the methods in the parent class, and also write the main function together.

1  Public classOverrideextendsextendsOverridetest {2     3 @Override4      Public voidtest1 () {5         //TODO auto-generated Method Stub6 //Super.test1 ();7System.out.println ("Method after subclass inheritance");8     }9     Ten      Public Static voidMain (string[] args) { One          AOverridetest ot =Newoverrideextends (); - Ot.test1 (); -          the     } -  -}

In this case, after running, the result is "after the subclass inherits the overridden method", that is, the subclass in the way of overriding the Test1 method in the parent class is overridden, when used, directly executes the method in the subclass.

Attention:

1. When overridden, the method name must be the same.

2. When overriding, the parameters must be the same.

3. When overridden, the return value must be the same. (or None)

Java overloads and overrides

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.