PackageCom.test2; Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubSystem.out.println (USB.A); //Create a computerComputer computer =Newcomputer (); //Create a CameraCamera camera1 =NewCamera (); //Create phonePhone phone1 =NewPhone (); Computer.useusb (CAMERA1); Computer.useusb (PHONE1); }}Interfaceusb{intA = 1; //two methods are declared in the interface//Start Working Public voidstart (); //Stop Working Public voidStop (); }//write the camera class and implement the USB interface//when a class implements an interface, it requires the class to implement all the interfaces of the class.classCameraImplementsusb{ Public voidstart () {System.out.println ("I'm a camera, I'm working."); } Public voidStop () {System.out.println ("I'm the camera, stop working."); }}//Write Phone classclassPhoneImplementsusb{@Override Public voidstart () {//TODO auto-generated Method StubSystem.out.println ("I'm a cell phone, getting to work"); } @Override Public voidStop () {//TODO auto-generated Method StubSystem.out.println ("I'm a cell phone, stop working"); } }classcomputer{//the interface also embodies polymorphic//Getting started with the USB interface Public voidUSEUSB (USB usb) {Usb.start (); Usb.stop (); }}
Interface---Java