overwriting and hiding of Java variables and methods (translated from Java tutorials)

Source: Internet
Author: User
Tags instance method

Hide Variables

In a class, if the name of a variable is the same as the name of a variable of the parent class, even if their type is different, then in the subclass, the same name variable of the parent class cannot be accessed directly by the variable name.
However, a variable with the same name as the parent class can be accessed by super. In general, hidden variables are not recommended, which makes the code difficult to read

overriding and Hiding methods Object instance Methods

In an object instance method, if a subclass has a method whose signature (the method name, the number and type of parameters of the method), and the return value are the same as the parent class, the method that overrides the parent class.
Subclass overrides the ability to allow a class to inherit the behavior of the parent class, modifying some behavior as needed. The overridden method, which corresponds to the parent class, has the same name, the same parameter type and number, and the same return type. Another overriding method can return a subclass of the return type of the method of the parent class. This is called the covariant return type.
When overriding a method, you will use the annotation @override to tell the compiler how you want to overwrite the parent class. However, if the method does not exist in the parent class, the compiler will make an error.

class Method

If a subclass declares a class method with the same signature as the parent class, the subclass hides the method of the parent class.
The difference between hiding and covering is of great significance. The invocation version of the overridden method is the method of the subclass. Hides the invocation version of a method, depending on whether it is called by the parent class or by the quilt class.

Here is an example of the difference between an object instance method and a class method, and the first class is animal

public class Animal {public    static void Testclassmethod () {        System.out.println ("the Class" + "method in Animal.") );    }    public void Testinstancemethod () {        System.out.println ("the instance" + "method in Animal.");}    }


The second class is cat, which is a subclass of animal:

public class Cat extends Animal {public    static void Testclassmethod () {        System.out.println ("The class method" + " In Cat. ");    public void Testinstancemethod () {        System.out.println ("The instance method" + "in Cat.");    }    public static void Main (string[] args) {        cat mycat = new Cat ();        Animal myanimal = mycat;        Animal.testclassmethod ();        Myanimal.testinstancemethod ();    }}

The Cat class covers the instance method of animal and hides its class methods. The resulting output is as follows:

The class method in Animal.the instance method in Cat.


As previously mentioned, because the hidden method is called through the parent class, the calling version of the Hidden method is the parent class. The invocation version of the overridden method is a subclass method.

modifier

The overridden access modifier can allow widening, but does not allow narrowing. For example, a protected object method can be modified in a subclass to public, but not private. Otherwise, the code will fail at compile time.

method Overlay and Hide Summary

The following table summarizes the various scenarios in which a subclass declares a method that is the same as the signature of a parent class

Superclass Instance method Superclass Static method
Subclass Instance Method Overrides Generates a compile-time error
Subclass Static Method Generates a compile-time error Hides

overwriting and hiding of Java variables and methods (translated from Java tutorials)

Related Article

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.