Overloading (overload) and overwrite (override) of Java methods

Source: Internet
Author: User
Tags modifier

Method overloading (overload) and overwrite (override)

Sometimes, the same function of a class is implemented in many ways, depending on the parameters given by the caller. For example, our most commonly used System.out.println () can print out data of any data type, and it has many implementations. At run time, the Java Virtual machine first determines the type of the given parameter and then decides which println () method to execute.

Overloading (Overload): For Methods of a class (including methods inherited from a parent class), the method name is the same, and the method of the parameter list differs from one another to form an overloaded relationship. Here are two questions to note:

(1) What is a parameter list? Parameter list is also called parameter signature, refers to three kinds of things: the type of parameters , the number of parameters , the order of the parameters . Each of these three is called a different parameter list, as long as there is a difference.

(2) Can an overloaded relationship only occur in the same class? Not also. At this time you have to understand the inheritance deeply, knowing that the members of a subclass have inherited the parent class in addition to their own explicit writing. Therefore, a method in a subclass and a method inherited from a parent class can also have overloaded relationships.

You need to tighten the definition to see if the method is overloaded, without the modifier and return type of the method and the exception thrown, just look at the method name and the argument list. And keep in mind that constructors can also be overloaded.

Override: Also called rewriting, which overrides a method of a parent class in a subclass when some methods in the parent class do not meet the requirements. When a method in a parent class is overwritten, the method in the parent class cannot be called unless the Super keyword is used.

Conditions in which coverage occurs:

1, the "three same not low " subclass and the parent class 's method names , the parameter list , the return type must be exactly the same , and the child class method access modifier permissions can not be more than the parent class Low .

Note: The covariant return type is added in Java SE5, where the overridden method in a subclass can return the subclass type of the type returned by the overridden method in the base class. For example, if the overridden method in the base class returns a subclass of type shape,circle, the override Method Getarea () in the subclass can return the circle type. The return type is not the same at this time.

2. The subclass method cannot throw more exceptions than the parent class method. That is, the exception thrown by the subclass method must be the same as the exception thrown by the parent method, or its subclass , or nothing ;

3. The overridden method cannot be of the final type. Because the final decoration method is not overwritten.

4. The overridden method cannot be private. Otherwise, only a new method is defined in its subclass, and it is not overwritten.

5. The overridden method cannot be static. So if the method in the parent class is static, and the method in the subclass is not static, the two methods, except this one, satisfy the overwrite condition, and a compilation error occurs. Vice versa. Even if the methods in the parent and child classes are static, and the overrides are met, the overwrite is still not occurring because the static method matches the static method and the reference type of the class at compile time.

The overrides and overloads of the method have the following same points:

Requires a method with the same name

Can be used between abstract and non-abstract methods

The overrides and overloads of a method have the following different points:

Method overrides require that the parameter list (parameter signature) must be consistent, and the method overload requires that the argument list be inconsistent.

Method overrides require that the return type be consistent, and the method overload does not require it.

Method overrides methods that can only be used by subclasses to override the parent class, and method overloads are used for all methods in the same class (including methods inherited from the parent class)

Method overrides have special requirements for access to methods and exceptions thrown, and method overloading has no limitations in this regard.

A method of a parent class can be overridden only once by a class, and a method can be overloaded multiple times in all classes.

In addition, for attributes (member variables), it is not overloaded and can only be overridden.

Note: You can add an annotation @override before the overridden method, plus the compiler will get an error when the method does not meet the overriding criteria for the parent class method (three same is not low).

 Public classFielddemo { Public Static voidMain (string[] args) {Student T=NewStudent ("Jack"); Person P= t;//a reference created by the parent class points to the object created by the child classSystem.out.println (t.name+ "," +p.name); System.out.println (T.getname ()+","+p.getname ()); }    }  classperson{String name; intAge ;  PublicString GetName () {return  This. Name; }  }  classStudentextendsperson{String name;//property is the same as the property name of the parent class, but it is generally not the same as the parent property name when doing development!!      PublicStudent (String name) { This. Name =name; Super. Name = "Rose";//Assigning a value to a property in a parent class    }       PublicString GetName () {return  This. Name; }  }  

The return result is:

Jack,rose
Jack,jack

The reason: In Java, properties are bound to types, and methods are bound to objects!

 Public classCar { Public voidTick () {Show (); }    Private voidShow () {System.out.println ("Fu Show"); }} Public classMyCarextendscar{ Public intnum = 8;  Public voidShow () {System.out.println ("Zi Show"); }} Public classDemo {/*** Main function entry *@paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method StubMyCar C=NewMyCar ();            C.tick (); }    }

Overloading (overload) and overwrite (override) of Java methods

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.