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