Java Foundation 10--interface One, definition and instance of interface
1 /*2 Abstract class Absdemo3 {4 abstract void Show1 ();5 abstract void Show2 ();6 }7 8 When a method in an abstract class is abstract, the abstract class can be defined and represented by another form of 9, which is the interface interface. Ten */ One A //the keyword used to define the interface is not class, it is interface. - //interface compiled or. class files are essentially class. - the /* - interfaces and classes are implementations, interfaces and interfaces are multi-inheritance, why can multiple inheritance, because there is no method body, can be more than the essence of inheritance see method body - */ - + /* - for members that are common to interfaces: and these members have fixed modifiers. 21 1, Global constant: public static final 22 23 2, abstract method. Public abstract - - If you don't write your own words, the program adds to you. So we all write, that's good reading. - - This concludes that the members in the interface are public permissions. - inInheritance does not implement member variables, methods are abstract methods, there is no method of the body of the function, so the implementation of interfaces and inheritance classes do not conflict, because the interface and the class is not the same thing - */ to InterfaceDemo + { - Public Static Final intNUM = 4;//Static and final and public you don't write programs will give you default plus the * Public Abstract voidShow1 ();//Public and abstract you don't write programs will give you default plus $ Public Abstract voidShow2 ();Panax Notoginseng } - //A class is an inheritance relationship with a class, and a class is a direct implementation relationship with an interface. the /* + interfaces cannot be instantiated. A the the subclass can be instantiated only by implementing the subclass of the interface and overriding all the abstract methods in the interface. + Otherwise, this subclass is an abstract class. - $ */ $ - - classDemoimplImplements /*Implement*/Demo the { - Public voidShow1 ()Wuyi {} the - Public voidShow2 () Wu { - About } $ } - - - /* A multiple inheritance is not directly supported in Java because of the uncertainty of the call. + so Java improved the multi-inheritance mechanism and became a multi-implementation in Java. the - A class can implement multiple interfaces. $ the */ the the InterfaceA the { - Public voidShow (); in } the the InterfaceZ About { the Public intAddintAintb); the } the + - the Bayi classTestImplementsA,z//Multi-Implementation the { the - Public intAddintAintb) - { the returnA+b+3; the } the /**/ the Public Void Show () {} - the } the the 94 the the the /*98 A class can also implement multiple interfaces while inheriting another class. About - 101 102 */103 classQ104 { the Public voidmethod ()106 {}107 }108 109 Abstract classTest2extendsQImplementsa,z the {111 the }113 /* the The presence of interfaces avoids the limitations of single inheritance. the the */117 118 119 InterfaceCC - {121 voidShow ();122 }123 InterfaceMM124 { the voidmethod ();126 }127 - InterfaceQqextendscc,mm//an interface is an inheritance relationship with an interface, and an interface can inherit multiple. 129 { the voidfunction ();131 } the 133 classWwImplementsQQ134 {135 //covers 3 methods. 136 Public voidShow () {}137 Public voidmethod () {}138 Public voidfunction () {}139 } $ 141 142 classInterfacedemo143 {144 Public Static voidMain (string[] args)145 {146 147Test T =NewTest ();148 t.show ();149 Max //Demoimpl d = new Demoimpl (); 151 //System.out.println (d.num); the //System.out.println (demoimpl.num);153 //System.out.println (demo.num);154 }155}
Ii. the difference between an interface and an abstract class
1 /*2 the similarities and differences between abstract classes and interfaces:3 4 Same point:5 Are constantly upward drawn. 6 7 8 different points:9 1, abstract classes need to be inherited, and can only be inherited . Ten interfaces need to be implemented and can be implemented more . One 2. Abstract methods and non-abstract methods can be defined in an abstract class, and after the subclass inherits, non-abstract methods can be used directly. A only abstract methods can be defined in an interface and must be implemented by subclasses. - 3, the inheritance of abstract class, is a relationship, in the definition of the basic common content of the system. - the implementation of the interface is like a relationship, which defines the system extra functionality . the - - - + dogs According to function: There are guide dogs, search explosive dogs. - + A Abstract class Dog at { - abstract void Roar (); - } - - //abstract class guided Blind - Interface Guide Blind in { - abstract void Guide (); to } + - the class guide dog extends dog implements guided blindness * { $ Public void Roar ()Panax Notoginseng { - } the Public Void Guide () {} + } A the + - //In different problem areas, there are different methods of analysis. $ Student: $ Learning. - Smoking Trainees - the - Wuyi the smokers. - Wu - About $ - - - A */ + the classInterfacedemo - { $ Public Static voidMain (string[] args) the { theSystem.out.println ("Hello world!"); the } the}
Java Foundation 10--Interface