Java polymorphism trap

Source: Internet
Author: User

According to the original, let's test the output results of the following program without running it. (Please note that this is about polymorphism)

Package acm; // Example 1 class Test extends Main {private void write () {System. out. println ("Test") ;}} public class Main {private void write () {System. out. println ("Main");} public static void main (String [] args) {Main main = new Test (); Main. write () ;}// we expect the output of Test, but the private method is automatically considered as the final method. It also shields exported classes. Therefore, // In this case, the write method in Test is a brand new method, and the method in Main is invisible in the subclass, therefore, it cannot be overloaded. // only non-private methods can be overwritten. So the result is: Main // Example 2 class Super {public int field = 0; public int getField () {return field ;}} class Sub extends Super {public int field = 1; public int getField () {return field;} public int getSuperField () {return super. field ;}} public class Main {public static void main (String [] args) {Super sup = new Sub (); System. out. println ("sup. field = "+ sup. field + ", sup. getField () = "+ sup. getField (); Sub sub = new Sub (); System. out. println ("sub. field = "+ sub. field + ", sub. getField () = "+ sub. getField () + ", sub. getSuperField () = "+ sub. getSuperField () ;}// when the Sub object is transformed to a Super reference, any domain access operations will be parsed by the compiler, so they are not polymorphism. // In this example, different buckets are allocated for Super. field and Sub. field. In this way, Sub actually contains two fields called field: // its own and its obtained from Super. Result: // sup. field = 0, sup. getField () = 1 // sub. field = 1, sub. getField () = 1, sub. getSuperField () = 0 // Example 3 class Glyph {void draw () {System. out. println ("Glyph. draw () ");} Glyph () {System. out. println ("Glyph () before draw ()"); draw (); System. out. println ("Glyph () after draw ()") ;}} class RoundGlyph extends Glyph {private int radius = 1; RoundGlyph (int r) {radius = r; System. out. println ("RoundGlyph. roundGlyph (), radius = "+ rad Ius);} void draw () {System. out. println ("RoundGlyph. draw (), radius = "+ radius) ;}} public class Main {public static void main (String [] args) {new RoundGlyph (5 );}} // when the Glyph builder calls draw (), the radius value is not even the default value of 1, but 0. // The initialization sequence described in the previous article is incomplete. The actual process is as follows: // (1) before taking any other operations, the bucket allocated to the object is initialized to binary zero. // (2) call the base class builder. In this case, the overwritten draw () method will be called (indeed before the RoundGlyph builder call), // The radius value is 0, which is due to step (1). // (3) Call the member initialization code in the Order stated previously. // (4) Call the entity class builder. // Result: // Glyph () before draw () // RoundGlyph. draw (), radius = 0 // Glyph () after draw () // RoundGlyph. roundGlyph (), radius = 5

All of the above are obtained by reading Thinking in Java recently.

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.