About Java up transformation

Source: Internet
Author: User

The recent review of the comparison of a place to summarize through the experiment

If a is a parent class, B is declared as a subclass:

A t= new B ();

Assigns a subclass object to the parent class object, which is compiled as a parent class object, but the runtime is a subclass object:

1) is declared as a parent object;

2) has the parent class property, calls the parent class the non-subclass overrides the method;

3) Occupy sub-class memory space;

4) Cannot access variables and methods peculiar to subclasses;

5) When a subclass overrides a method of the parent class, the method of the child class is called.

1 classSubtestextendstest{2     intx, y;3      Public voidPrinthello () {4System.out.println ("hello!!");5     }6      Public voidprinthhe () {7         8     }9 }Ten  Public classTest { One     intme; A      Public voidPrinthello () { -System.out.println ("Hello"); -     } the      Public voidPrinthi () { -System.out.println ("Hi"); -     } -      Public Static voidMain (string[] args) { +          -Test a=Newsubtest (); + System.out.println (a.me); ASystem.out.println (a.x);//compilation error cannot have subclass attribute atA.printhello ();//at this point the output is subclass "hello!! " - A.printhi (); -A.printhehe ();//compilation error cannot call subclass method -     } -  -}

About Java up transformation

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.