Java face question Super.getclass () method call

Source: Internet
Author: User

Tag:port   math   import   color    code      Object Type     interview    end   test   

  What is the output of the program below?   import   java.util.Date;  public  class  Test extends   date{ public  static  void   main (string[] args) { new   Test (). Test ();   public  void   Test () {System.out.println ( super  .getclass (). GetName ()); }  }  

In the test method, the GetClass (). GetName () method is called directly, and the test class name is returned. Because GetClass () is defined as final in the object class, the subclass cannot overwrite the method, so the GetClass (). GetName () method is called in the test method, which is actually called the GetClass () method inherited from the parent class. is equivalent to calling the Super.getclass (). GetName () method, so the Super.getclass (). GetName () method returned should also be test.you might think of date, but the actual result is test. Yes, you're not mistaken, super.getclass () does not return a reference to a superclass. Let's do another experiment, calling the GetClass (). GetName () method directly in the test method, and the result is test. Why doesn't super play a role? Simply put, super does not represent a reference to a superclass. Because Super does not have the ability to represent a reference to a superclass, it simply represents the method that invokes the parent class. Therefore, in a subclass method, you cannot use SYSTEM.OUT.PRINTLN (super) or Super.super.mathod (); In fact, Super.getclass () is the method that represents the invocation of the parent class. The GetClass method comes from the object class, which returns the type of objects at run time. Because the object type at run time is test, both This.getclass () and Super.getclass () are returned as test. In additionbecause GetClass () is defined as final in the object class, the subclass cannot overwrite the method, so the GetClass (). GetName () method is called in the test method, which is actually called the GetClass () method inherited from the parent class. is equivalent to calling the Super.getclass (). GetName () method, so the Super.getclass (). GetName () method returned should also be Test. If you want the name of the parent class, you should use the following code:Java codegetclass (). Getsuperclass (). GetName ();

Java face question Super.getclass () method call

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.