Inheritance of Java object-oriented programming (II.)

Source: Internet
Author: User
Tags instance method

In our previous blog, we already know the basic concepts and syntax of inheritance, so today we'll talk about other things about inheritance.

Let's take a look at what is method overloading (overload) and method override (override) method overloads (Overload)

For methods of the class, including methods inherited from the parent class, if the method name of the two methods is the same, but the parameters are inconsistent, then one method is the overloaded method of the other method.

The overloaded method must meet the following conditions:

    • Method name is the same
    • The parameter type, number, and order of the method are at least one item
    • The return type of the method can be different
    • The modifiers of the method can be different

Where the type, number, and order of the parameters are collectively referred to as parameter signatures. Consider the example:

 Public classexample{ Public voidAmethod (intI, String s) {}//instance Method//Add other methods     Public voidAmethod (intI,string s) {}//No, because this method is exactly the same as the instance method we defined, the Java Virtual machine does not know which one to execute when executing     Public voidAmethod (String S,inti) {}//Yes, the overloaded method, the order of the parameters is different     Public intAmethod (intI,string s) {return0;}//No, although the return type of the method is not the same, the method name and parameter signature are exactly the same, so this is not an overloaded method and cannot be added to example     Public PrivateAmethod (intI,string mystring) {}//No, the method name and parameter signature are the same, so this is not an overloaded method     Public voidAmethod (intI,string s) {}//The method name is not the same as a new instance method, so it can be added to the example class    Abstract voidAmethod (inti) {}//No, the method has a different number of arguments than it already has, so it is an overloaded method. But the example class is not an abstract class, and all cannot be added to this abstract method. If you change the example class to an abstract class. You can add this method}

In addition, the main () method, which is a program entry, can be overloaded.

Method override (Override)

If a method defined in a subclass, whose name, return type, and parameter signature matches exactly the name, return type, and parameter signature of a method in the parent class, then it is said that the method of the subclass overrides the method of the parent class.

The override method must meet a variety of constraints, described below:

  1. The name, parameter signature, and return type of the subclass method must match the name of the parent class method, the parameter signature, and the return type.
  2. What does it mean that a subclass method cannot narrow the access rights of the parent method? We can look at the following example:
     Public class extends b{    privatevoid// Compile error, subclass method reduces access rights for parent method }class  b{    publicvoid  method () {}}

    The method () methods of the subclass are private, and the method () methods in the parent class are public, and the subclasses reduce access to the parent method, which is an invalid method override that causes a compilation error.

  3. A subclass method cannot throw more exceptions than a parent class method. The subclass method throws an exception that must be the same as the exception thrown by the parent class method, or a subset of the exception thrown by the parent class method.
  4. Method overrides exist only between subclasses and parent classes (including direct and indirect parent classes), and methods in the same class can only be overloaded and cannot be overwritten.
  5. A static method of a parent class cannot be overridden by a quilt class as a non-static method.
  6. A subclass can define a static method with the same name as a static method of the half class so that the static method of the parent class is hidden in the child class. At compile time, the static methods defined by the subclass must also satisfy and override similar constraints: The method's parameter signature is consistent, the return type is consistent, the access rights of the parent method cannot be reduced, and more exceptions cannot be thrown.
  7. A non-static method of a parent class cannot be overridden by a quilt class as a static method.
  8. Private methods of the parent class cannot be overridden by the quilt class.
  9. The abstract method of the parent class can be overridden in two ways by subclasses: One is the abstract method of implementing the parent class of the subclass, and the other is the abstract method of the subclass to re-declare the parent class. For example, the following code is valid:
Abstract classBase () {Abstract voidmethod1 (); Abstract voidmethod2 ();} Public Abstract classSubextendsBase () { Public voidMethod1 () {...};//Implement the Method1 () method and expand the scope of access     Public AbstractMethod2 () {};//re-declare the METHOD2 () method, just expand access, but do not implement}

10. A non-abstract method of the parent class can be overridden as an abstract method.

In a narrow sense, overriding refers only to the specific method by which the subclass overrides the parent class, that is, the non-abstract method, which provides the default implementation of the method in the parent class, and the subclass uses a different implementation way.

The similarities and differences between method overrides and method overloads

Method overrides and method overloads have the following same points:

    • Requires a method with the same name.
    • Can be used between abstract methods and non-abstract methods.

Method overrides and method overloads have the following different points:

    • Method overrides require that parameter signatures must be consistent, and method overloading requires parameter signatures to be inconsistent.
    • Method overrides require that the return type be consistent, and the method overload does not limit it.
    • Method overrides methods that can only be used by subclasses to inherit from a parent class, and method overloads are used for all methods of 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 respect.
    • A method of a parent class can be overridden only once by a class, and a method may be overloaded multiple times in the class in which it resides.

Inheritance of Java object-oriented programming (II.)

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.