The differences between Java overloading, overriding, and hiding

Source: Internet
Author: User

J the difference between Ava overloading, overriding, and hiding

first, overload ( Overload)

Note: In order to correctly differentiate between overloading and rewriting, please be sure to remember the overloads ( overload) and rewrite (override) in English.

(1) overload definition: Indicates that two or more methods have the same name, but the parameters of the method are different. The method parameter has two meanings: A, the number of parameters is different; B, the type of the parameter is different.

[Java] view plain copy

    1. Public void Run (String name, int count)
    2. {
    3. SYSTEM.OUT.PRINTLN (count + "+ name +" + "is running.");
    4. }
    5. Public void Run (int count, String name)
    6. {
    7. SYSTEM.OUT.PRINTLN (count + "+ name +" + "is running.");
    8. }

Consider whether the above two methods constitute overloading?

(2) When an overload occurs: an overload occurs between two or more methods within the same class.

(3) The return type of the method has no effect on overloading! that is , for the method name and parameters are the same two methods, if their return type is different, this syntax format is wrong, you can try. So, that is, the overload return type must be the same as long as it is. Otherwise, there are two different methods. The following code, which is two different methods.

[Java] view plain copy

    1. Public void Run ()
    2. {
    3. System.out.println ("Dog is Running");
    4. }
    5. Public int Run (String name, int count)
    6. {
    7. SYSTEM.OUT.PRINTLN (count + "+ name +" + "is running.");
    8. return 1;
    9. }

(3) overloading between constructor methods: There are multiple constructors in a class, and they must be overloaded.

(4) call between overloaded methods: for overloads of normal methods, the overloaded methods can be called directly, as in code one below.

However, for calls between overloaded construction methods, the This () keyword is required. A, this () parameter in parentheses indicates the parameters received by the target construction method; B, the call to this () must be the first statement of the constructor method. Code two below.

[Java] view plain copy

    1. Public int Run (String name, int count)
    2. {
    3. SYSTEM.OUT.PRINTLN (count + "+ name +" + "is running.");
    4. return 1;
    5. }
    6. Public void Run (int count, String name)
    7. {
    8. Run (name, count);
    9. SYSTEM.OUT.PRINTLN (count + "+ name +" + "is running.");
    10. }

[Java] view plain copy

    1. Public Dog ()
    2. {
    3. This ("Kimba");
    4. System.out.println ("Hello World, I am a dog!") ");
    5. }
    6. Public Dog (String name)
    7. {
    8. System.out.println ("Hello World," + "I am a" + name + "Dog!");
    9. }

second, rewrite ( Override)

(1) rewrite definition: The subclass is the same as the return type of the parent class's method, the method name is the same as the parameter, so we say that the subclass and the method of the parent class constitute an overriding relationship.

add: In the rewrite, we have never mentioned the issue of permissions (public, protected, private), the use of private decoration method is not able to rewrite, and can not be inherited, so private and polymorphic pull relationship. Under the same package, the methods that make up the rewrite relationship, the parent class, and the subclass, permissions can be public and public, protected and protected, protected, and public, but not public and protected, That is, permissions can be expanded but not scaled down.

(2) When the rewrite occurs: overrides occur between the parent and child classes.

(3) constructor method cannot be overridden, if you understand the role of the constructor method, you know why the construction method cannot be overridden.

(4) When overridden, when a subclass overrides a method called by the parent class, it can be called using the Super. Parent class method, where this statement does not have to be placed on the first line. As shown in the following code:

[Java] view plain copy

[Java] view plain copy

    1. class Animal
    2. {
    3. Public void Run ()
    4. {
    5. System.out.println ("Animal is running");
    6. }
    7. }
    8. class Dog extends Animal
    9. {
    10. Override
    11. Public void Run ()
    12. {
    13. System.out.println ("Dog is Running");
    14. Super. Run ();
    15. }
    16. }

third, Hidden ( Hide)
(1) refers to the hidden, you have to mention a keyword--static,static is static meaning, the specific use of meaning, you can check the relevant information. Here I would like to say, the static method in the inheritance of the particularity!
(1) subclasses cannot rewrite (overriding) The static methods of the parent class, only the static methods of the parent class (hiding) can be hidden.

(2) What is a static method that hides a parent class? That is to say, the static method of the parent class and the child class is simultaneous, which method is called, is to see what kind of reference is called the method reference, if it is a reference to the parent type, the call is a static method of the parent class, if it is a reference to a subtype, the call is a static method of the subclass. "There are some polymorphic (polymorphism) knowledge, do not understand can ask me ha!" "We can see an example of the exact difference, and this example comes from an official document.

[Java] view plain copy

    1. Public class Animal
    2. {
    3. Public Static void Testclassmethod ()
    4. {
    5. System.out.println ("The Class" + "method in Animal.");
    6. }
    7. Public void Testinstancemethod ()
    8. {
    9. System.out.println ("The instance" + "method in Animal.");
    10. }
    11. }

The second class, a subclass of Animal, is called Cat:

[Java] view plain copy

  1. Public class Cat extends Animal
  2. {
  3. Public Static void Testclassmethod ()
  4. {
  5. System.out.println ("The class method" + "in Cat.");
  6. }
  7. Public void Testinstancemethod ()
  8. {
  9. System.out.println ("The instance method" + "in Cat.");
  10. }
  11. Public Static void Main (string[] args)
  12. {
  13. Cat Mycat = new cat ();
  14. Animal myanimal = Mycat;
  15. Animal.testclassmethod ();
  16. Myanimal.testinstancemethod ();
  17. }
  18. }

Hide: when a parent class or subclass has a property or method with the same name, the property or method form of the parent class is missing, and the actual is still there.

Shadowing occurs between a child class and a parent class, and is hidden for member variables and static methods in the parent class.

When a subclass declares a variable with the same variable name as a member variable in the parent class, it implements the hiding of the member variable in the parent class;

When a subclass declares that a static member method in the parent class has the same method name, a parameter list, and the same return value, it implements the hiding of the static method in the parent class.

 Note: when a hidden event occurs, the declared type is a class that invokes the property or method of the corresponding class without dynamic binding

    method hiding has only one form, that is, the parent class and subclass have the same static method

    properties can only be hidden and cannot be overwritten

    Child class instance variable / Static variables can hide instances of the parent class / static variables, summarized as variables can be cross-hidden

The difference between hiding and overwriting:

Properties that are hidden are accessed by attributes in the parent class after the child class is cast to the parent class

Overridden method that is called by the subclass itself after the subclass is cast to the parent class

  because overrides are dynamic bindings and are subject to The RTTI (runTime type identification, runtime types check ) constraints, hidden from RTTI constraints, are summarized as RTTI For overrides only, not for hidden

Special cases:

  1.final Modified properties can be hidden, but cannot be assigned value, that is, can not be used = To assign a value, the web said the final property can not be modified, this statement is not accurate, Because a variable of a reference type is final decorated, it simply cannot be pointed to another object, but it can be changed to its own value, which can be tested with ArrayList. The final property can be initialized at run time, but the initialization statement cannot occur

  2.final modified methods cannot be overwritten and can be overloaded

  3.final -Modified classes cannot be inherited

  the 4.private method implicitly adds the final

Instance:

Superclass class:

    Package COM.YILONG.TEST.SCJP;

  1. Public class Superclass {
  2. Public Static int i = 1;
  3. Public int j = 2;
  4. Public Final int k = 3;
  5. Public Static void method1 () {
  6. System.out.println ("Superclass Method1");
  7. }
  8. Public void method2 () {
  9. System.out.println ("Superclass Method2");
  10. }
  11. Public Final void method3 () {
  12. System.out.println ("Superclass Method3");
  13. }
  14. }

The differences between Java overloading, overriding, and hiding

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.