Java Super.getclass Issues

Source: Internet
Author: User

A very famous pit, the simple description is to call Super.get () in a method in subclass B of A. GetName (); Do you get A or B?

The answer is to know, B. Many of the statements on the Internet are reversed, such as the following example:

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 ());}    }

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " The > proves that the idea is that the GetClass () method is the final method of object and cannot be overridden for inheritance. This means that it can be inherited, but cannot be rewritten by itself. Then call GetClass () in the test class. GetName (); Get Test </span>

Class name, because it is not rewritten, so can only be inherited, that is, in Test call GetClass is equal to call super.getclass;

But I always felt that the explanation did not persuade me, and then went on to look at the information and found such an explanation:

/**     * Returns the runtime class of this {@code Object}. The returned     * {@code Class} object is the object, the locked by {@code     * static Synchronized} methods of the R Epresented class.     **/

This is a comment for the GetClass method in object, which means that the return running theClass.

The interpretation states that to return this object runtime class, it is certainly not possible to refer to object itself, otherwise all classes call the GetClass () method to return Object.class

This is easier to understand. Because GetClass is native, that is, Java itself has been implemented, the purpose is very clear is to return the current running object class information, note is running, above the test object is running, of course, the return is the test class information.

A simple verification example is attached below:

public class Cat {public final void speak () {System.out.println ("the class===" +getclass (). GetName ());}}
public class RedCat extends cat{}
public class Smallredcat extends redcat{public void Test () {super.speak ();} public static void Main (string[] args) {smallredcat rc = new Smallredcat (); Rc.test (); RedCat RedCat = new RedCat (); Redcat.speak ();}}

The operating result is:

The Class===com.test.smallredcat
The Class===com.test.redcat

Reference: http://www.cnblogs.com/o-andy-o/archive/2012/12/22/2829563.html

Java Super.getclass Issues

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.