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