Overwrite and hide Java variables and methods (translated from Java tutorials)

Source: Internet
Author: User
Hide Variables

In a class, if the name of a variable is the same as that of the parent class, even if their types are different, in the subclass, variables with the same name of the parent class cannot be accessed directly through the variable name.
However, variables with the same name of the parent class can be accessed through super. Generally, hidden variables are not recommended.CodeHard to read

 

Overwrite and hide Methods Object instance method

In an object instance method, if a subclass has a method, its signature (method name, number and type of parameters of the method) and return values are the same as those of the parent class, the method overwrites the parent class.
The ability of sub-classes to override. A class can inherit the behavior of the parent class and modify certain behaviors as needed. The override method has the same name as the method corresponding to the parent class, the same parameter type and number, and the same return type. Another override method can return the subclass of the return type of the method of the parent class. This is called the covariant return type.
When overwriting a method, you need to use the annotation @ override to tell the compiler that you want to override the method of the parent class. However, if the method does not exist in the parent class, the compiler reports an error.

 

Class Method

If the subclass declares a class method with the same signature as the parent class, the subclass hides the parent class method.
The difference between hiding and overwriting is of great significance. The called version of the override method is the subclass method. The call version of the hidden method depends on whether the method is called by the parent class or the quilt class.

The following example shows the difference between the object instance method and the class method. The first class is animal.

Public ClassAnimal {Public Static VoidTestclassmethod () {system. Out. println ("The Class" + "method in animal .");}Public VoidTestinstancemethod () {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 overwrites the animal instance method and hides its class method. The output is as follows:

 
TheClassMethod in animal. The instance method in cat.

As mentioned earlier, because the hidden method is called through the parent class, the called version of the hidden method is the parent class. The called version that overwrites the method is a subclass method.

 

Modifier

The overwrite access modifier can be expanded, but cannot be reduced. For example, a protected object method can be modified to public in the subclass, but not private. Otherwise, the code will fail during compilation.

 

Summary of method coverage and hiding

The following table summarizes various situations in which the subclass declares the same method as the parent class signature.

  Superclass instance method Superclass static method
Subclass instance method Overrides Generates a compile-time error
Subclass static method Generates a compile-time error Hides

 

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.