Java Polymorphism (i)

Source: Internet
Author: User

Parent class: Public classParent { PublicString name; PrivateString Pass;  Public voidsay1 (aa aa) {System.out.println ("Parent Say1"); } @SuppressWarnings ("Unused")    Private voideat () {System.out.println ("Parent Eat"); }     PublicString Getpass () {returnPass; }     Public voidSetPass (String pass) { This. pass =Pass; }} Subclass: Public classChildextendsparent{ Public Static voidMain (string[] args) {AA a=NewAA (); /*** Upward Transformation: When a program runs a call to a method, first see if the parent class has a calling method, * if any, call the method in the subclass, and if the parent class does not have this method, compile the error. *          */Parent P=NewChild ();                P.say1 (a); /*** down transformation, compile and run without error, here the downward transformation is safe. */ Child Child=(child) p;        Child.say1 (a);                Child.say2 (a); /*** Downward transformation, compile error-free, run times wrong: java.lang.ClassCastException * unsafe downward transformation. */        //parent P2 = new parent (); //Child C2 = (child) P2; //C2.say1 (a);    }         Public voidsay1 (aa aa) {System.out.println ("Child Say1"); }         Public voidsay2 (aa aa) {System.out.println ("Child Say2"); }}classAA { Public voidsys () {System.out.println ("A is"); }}classBbextendsaa{ Public voidsys () {System.out.println ("B is"); }}

Java Polymorphism (i)

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.