Java Override/overload

Source: Internet
Author: User

Override (Override)

Overrides are subclasses of the implementation of the method that allows access to the parent class to be rewritten! Neither the return value nor the formal parameter can be changed. That is, the shell does not change, the core rewrite!

The benefit of overriding is that subclasses can define their own behavior as needed.

That is, subclasses can implement methods of the parent class as needed.

In object-oriented principles, rewriting means that any existing method can be rewritten. Examples are as follows:

classanimal{ Public voidMove () {System.out.println ("Animals can move."); }}classDogextendsanimal{ Public voidMove () {System.out.println ("Dogs can run and walk."); }} Public classtestdog{ Public Static voidMain (String args[]) {Animal a=NewAnimal ();//Animal ObjectAnimal B =NewDog ();//Dog ObjectA.move ();//methods for executing the Animal classB.move ();//methods for executing the Dog class   }}

The results of the above example compilation run as follows:

Animals can move dogs can run and walk

As you can see in the example above, although B belongs to the animal type, it runs the Move method of the dog class.

This is because in the compile phase, only the reference type of the parameter is checked.

At run time, however, the Java Virtual machine (JVM) specifies the type of object and the method that runs the object.

So in the example above, the reason for compiling successfully is because the Move method exists in the animal class, but at run time, the method of the particular object is running.

Consider the following example:

classanimal{ Public voidMove () {System.out.println ("Animals can move."); }}classDogextendsanimal{ Public voidMove () {System.out.println ("Dogs can run and walk."); }    Public voidbark () {System.out.println ("Dogs can bark."); }} Public classtestdog{ Public Static voidMain (String args[]) {Animal a=NewAnimal ();//Animal ObjectAnimal B =NewDog ();//Dog ObjectA.move ();//methods for executing the Animal classB.move ();//methods for executing the Dog classB.bark (); }}

The results of the above example compilation run as follows:

Testdog.java:30: Cannot find Symbolsymbol  class  Animal                b.bark ();       ^

The program throws a compilation error because the reference type of B animal does not have a bark method.

overriding rules for methods
    • The argument list must be exactly the same as the overridden method;
    • The return type must be exactly the same as the return type of the overridden method;
    • Access permissions cannot be higher than the overridden methods in the parent class. For example, if a method of a parent class is declared public, overriding the method in a subclass cannot be declared as protected.
    • A member method of a parent class can only be overridden by its subclasses.
    • A method that is declared final cannot be overridden.
    • A method declared as static cannot be overridden, but can be declared again.
    • Subclasses and parent classes in the same package, subclasses can override all methods of the parent class, except for the methods declared private and final.
    • Subclasses and parent classes are not in the same package, the subclass can only override non-final methods that are declared public and protected by the parent class.
    • The overridden method can throw any non-mandatory exception, regardless of whether the overridden method throws an exception. However, the overridden method cannot throw a new mandatory exception, or a more extensive mandatory exception than the overridden method declaration, or vice versa.
    • The construction method cannot be overridden.
    • If you cannot inherit a method, you cannot override this method.
Use of the Super keyword

Use the Super keyword when you need to call the overridden method of a parent class in a subclass.

classanimal{ Public voidMove () {System.out.println ("Animals can move."); }}classDogextendsanimal{ Public voidMove () {Super. Move ();//How to apply the Super classSystem.out.println ("Dogs can run and walk"); }} Public classtestdog{ Public Static voidMain (String args[]) {Animal b=NewDog ();//Dog ObjectB.move ();//methods for executing the dog class   }}

The results of the above example compilation run as follows:

Animals can move dogs can run and walk
Overloading (overload)

Overloading (overloading) is a class in which the method name is the same and the parameters are different. What about the return type? can be the same or different.

Each overloaded method (or constructor) must have a unique list of parameter types.

Constructors can only be overloaded

Overloading rules

    • The overloaded method must change the parameter list;
    • The overloaded method can change the return type;
    • Overloaded methods can change the access modifier;
    • Overloaded methods can declare new or broader check exceptions;
    • Methods can be overloaded in the same class or in a subclass.
Instance
 Public classOverloading { Public intTest () {System.out.println ("Test1"); return1; }      Public voidTestinta) {System.out.println ("Test2"); }         //The following two parameter types are in different order     PublicString Test (inta,string s) {System.out.println ("Test3"); return"Returntest3"; }          PublicString Test (String s,inta) {System.out.println ("Test4"); return"Returntest4"; }          Public Static voidMain (string[] args) {overloading O=Newoverloading ();        System.out.println (O.test ()); O.test (1); System.out.println (O.test (1, "Test3")); System.out.println (O.test ("Test4", 1)); }}
The difference between overriding and overloading
Distinguishing points Overloaded Methods overriding Method
Parameter list Must be modified Must not be modified
return type can modify Must not be modified
Abnormal can modify can be reduced or deleted, must not throw new or wider exceptions
Access can modify Must not be more restrictive (can reduce the limit)

Java Override/overload

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.