PackageCn.china;Abstract classabsdemo{Abstract voidShow1 (); Abstract voidshow2 ();}/*** When a method in an abstract class is abstract, you can define and represent the abstract class in another form, that is, the interface interface * * Defines that the key used by the interface is not class, it is interface. **/Interfacedemo5{ Public Static Final intNumber=4; Public Abstract voidShow1 (); Public Abstract voidshow2 ();}/*** Classes and classes are inheritance relationships, and classes and interfaces are implementation relationships. * Interface cannot be instantiated * This subclass can be instantiated only by implementing the subclass of the interface and overriding all the abstract methods in the interface * Otherwise, the subclass is an abstract class*/classDemoimplImplementsdemo5{ Public voidShow1 () {} Public voidShow2 () {}}/*** For common members of the interface, and these members have a fixed modifier * 1, Global constants: public static Final * 2, abstract method publicly abstracted * This concludes that the members in the interface are common permissions. *//*** Multiple inheritance is not supported in Java because of the uncertainty of the call. * So Java makes multiple inheritance improvements, and Java becomes a multi-implementation. * A class can implement multiple interfaces*/Interfaceaa{ Public Abstract voidshow6 (); }Interfacebb{ Public Abstract voidshow6 ();}classTest1Implementsaa,bb{ Public voidshow6 () {}}/*** One class can also implement multiple interfaces * interfaces and interfaces while inheriting another class*/classq{ Public voidmethod () {}}classTest2extendsQImplementsaa,bb{ Public voidshow6 () {}Interfacecc{voidShowa (); } Interfacemm{voidSHOWB (); } InterfaceQqextendscc,mm{//interfaces and interfaces can inherit multiple relationships, and interfaces can inherit multiple voidfunction (); } classWwImplementsqq{ Public voidShowa () {} Public voidShowb () {} Public voidfunction () {}}} /*** Abstract class and interface similarities and differences: * The same point: all are constantly upward drawn from the * different points: * 1, the abstract class needs to be inherited from the abstraction, the interface needs to be implemented implements * 2, abstract class can be defined abstract methods and non-abstract methods, such inheritance can be directly Use non-abstract methods. * The interface can only define abstract methods, must be implemented by subclasses * 3, abstract class is a relationship in the definition of the system common capacity; interfaces are like a relationships that define the additional functionality of the system. * * Example: Students have the learning function can be extracted to define abstract classes; * Student smoking function, is extra function using interface interface Definition * * Example: * Dogs by function, with guide dog, search dog * Abstract class Dog * ABS tract void roar (); *} * Class guide Dog extends Dog implements guide {* public void roar () {} * public void Guide () {} *} * Interface Guide {* Abstract Vo ID guide blind (); * }*/ //in different problem areas, there are different methods of analysis. /*** Notebook computer use, * in order to expand the function of the notebook, and what will appear in the future features of the device do not know * * Define a rule, as long as the future equipment will conform to this rule can be * Rules in Java is the interface interface*///class mouse{// // }//class bookpc{//Public static void Main (String[]args) {//Usemouse (New Mouse ());// }//Public static void Usemouse (Mouse m) {//M.open ();// }//} Interfaceusb{//storm-Drain Rules Public Abstract voidopen (); Public Abstract voidclose ();} Public classinterface{ Public Static voidMain (String[]args) {//Demoimpl d=new Demoimpl ();//System.out.println (d.number);//System.out.println (demoimpl.number);//System.out.println (Demo.number); //TODO auto-generated Method Stub//Test1 t=new Test1 ();//t.show6 (); //useusb (null);Upan u=NewUpan (); Useusb (U);//features Extend the } //usage Rules Public Static voidUseusb (USB u) {//a reference to an interface type that receives (points to) an object of an interface subclass. U.open (); U.close (); } //implementation Rules classUpanImplementsusb{ Public voidOpen () {System.out.println ("Upan Open"); } Public voidClose () {System.out.println ("Upan Close"); } } }
Java====interface interface