Java-day13-implementdemo

Source: Internet
Author: User

Application cases of interface implementation, inheritance and other relations
P.S: Cast to see the relationship between the object that the reference variable is pointing to and the target data.
You can use the reference variable instanceof target data to determine if a strong turn is available
1  PackageCom.implementdemo;2 3 /*application of interface implementation, inheritance and other relations*/4  Public classImplementdemo {5      Public Static voidMain (string[] args) {6Abs x =NewAoo ();7 8         /*9 Aoo xx = (Aoo) x; Full access to Aoo, strong turnTen x.b (); One xx.a (); A x.a (); -         */ //Cannot access -  the         /* - * Cast to see the object that the reference variable points to, - * that is to see the relationship between the object and the target data -          * */ +         if(xinstanceofBoo) { -Boo xx =(Boo) x; + xx.b (); Axx.num2 = 5;//classcastingexception type Conversion exception at         } -  -         if(xinstanceofInter1) { -Inter1 xx =(Inter1) x; - xx.a (); -         } in  -Inter1 y =NewAoo ();//Upward Styling to     } + } -  the Interfaceinter1{ *     Abstract voida (); $ }Panax Notoginseng Abstract classabs{ -      Public Abstract voidb (); the } +  A classAooextendsAbsImplementsinter1{ the     intNUM1; +      Public voidA () { -System.out.println ("a"); $     } $      Public voidB () { -System.out.println ("B"); -     } the } - classBooextendsAbs {Wuyi     intnum2; the      Public voidB () {} -}

Java-day13-implementdemo

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.