Rewrite/hide in Java and C + +

Source: Internet
Author: User

First, unrelated overloads .

Note: overloads are between functions of the same class. overrides are between child classes of the parent class. The difference between overload and overwrite.

The main talk here is the function rewriting and hiding

First of all, I understand that rewriting and hiding are mutually exclusive and relative. A function that exists in both parent and child is either a rewrite or a hidden one.

The essential difference between overriding and hiding is that overrides are dynamically bound , depending on the actual type of object that the runtime reference points to to invoke the members of the related class. While hiding is statically bound , the associated member of the call is determined based on the static type referenced at compile time. In other words, if a subclass overrides a method of a parent class, a subclass method is called by reference to the parent class when the parent class's reference points to the child class object. If a child class hides a method (member variable) of the parent class, the parent class's method (member variable) is called by reference to the parent class.

( note : This sentence is very much around, that is, the subclass hides the method of the parent class, but the method called by the parent class is more than the method of the parent class hiding the child class.) In fact, it is the hidden meaning of the subclass reference, which refers to the subclass reference called subclass, not the parent class, and the parent class reference still calls the parent class)

There is a difference between Java hiding and C + + hiding. Can not be said to be completely different, but the coverage and default adoption method is different.

First of all, Java hiding (see Link)

Overwrite refers to the parent class refers to the child class object, called when the child class is called the specific method;
  hiding refers to the subclass that hides the properties or methods of the parent class, and is about to  When the subclass is cast to the parent class, The properties and methods of the parent class are called . (The quotation mark is apt to cause ambiguity, can be ignored)  (1   )   variable  can only be hidden ( Both static and non-static), cannot be overwritten  (2)  You can hide static variables of a parent class with a static variable of a subclass, or you can hide a static variable of the parent class with a non-static variable of the subclass. You can also hide the final variable in the parent class with a non-final variable (final );  (3)  static method  (static    can only be hidden and cannot be overwritten; Span style= "Background-color: #ffff00" > (4    non-static method can be overridden ; (5 ) You cannot hide a non-static method in a parent class with a static method of a subclass, or the compilation will error, (6 ) You cannot overwrite the static method of the parent class with a non-static method of the subclass, or the compilation will error; (7) You cannot override the final method (final ) in the parent class; Abstract methods must be overridden in a specific class;         

Simply put, the static nature of the methods of the parent and child classes must be the same . Either there is static, or none, otherwise it will error, has been tested.

example, I experimented on IntelliJ, as follows:

PackageCom.company;classSolution {}classSuperclass {public static int i = 1; public int J = 2; public final int k = 3; public static voidMethod1 () {System.out.println ("Superclass Method1")); } public voidMethod2 () {System.out.println ("Superclass Method2")); } public final voidMethod3 () {System.out.println ("Superclass Method3")); }}class Subclass extendsSuperclass {public static int i = 2;//can hide the parent class's variable i public static int j = 1, whether it is static or not; public final int k = 4;//can hide the parent class's variable k public static void, whether final or not method1 () {System.out.println ("subclass Method1" ), public void  method2 () {System.out.println (" Subclass Method2 "); }/*public final void method3 () {System.out.println ("superclass Method3");} */}public class  main {public static void main (string[] args) throws  interruptedexception {superclass S c = new  subclass (); System.out.println ("i =" + sc.i); All member variables can only be hidden System.out.println ("j =" +  SC.J); System.out.println ("k =" +  SC.K); SC.METHOD1 ();//static method can only be hidden  sc.method2 (); Subclass SUBC = new  subclass (); System.out.println ("i =" +  subc.i); System.out.println ("j =" +  SUBC.J); System.out.println ("k =" +  SUBC.K); Subc.method1 (); SUBC.METHOD2 (); Your Codec object would be instantiated and called as such://system.out.printf ("ret:%d\n", ret);  System.out.println ();}}             

Printing results:

i = 1j = 2k = 3superclass Method1subclass method2i = 2j = 1k = 4Subclass Method1subclass method2
          

Remove the static and final variables from the above sub-categories:

    public int i = 2; //Whether it is static or not, it can hide the variable i    of the parent class public static Int j = 1;    public int k = 4; //Whether it is final, can hide the parent class variable K 

The printed result is consistent with the original:

i = 1j = 2k = 3superclass Method1subclass method2i = 2j = 1k = 4Subclass Method1subclass Met HOD2     

In C + + inside the hidden, and Java inside the hidden semantics, not quite the same, refer to Link:

If a function of a derived class has the same name as a function of a base class, the parameters are different. At this point, the function of the base class is hidden, regardless of the virtual keyword (Note that it is not confused with overloading).

In other words, C + + overrides are only related to the virtual keyword . without this keyword, the methods and subclasses in the parent class are not related. Even with virtual, if the method parameters are different, they are not overloaded, but hidden.

Java, by default, is overloaded , and only static methods and variables are not overloaded, but hidden.

Note: I think, for "hiding", the good memory method is to point to the child class instance of the parent class pointer (reference), see still is the method of the parent class, and the method of the class to "hide".

Rewrite/hide in Java and C + +

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.