Access rights for 1.Java interfaces
1 Interfacea{}//interface A package access rights2 3 Public Interfacea{}//interface a public access45 Interfacea{6 7 voidfunction1 ();8 9 Public voidFunction2 ();//Function1,function2 are public, that is, the default method of the interface is public and must be public.
In addition, the domain (member variable) in the interface is static,final by default.Ten //It has to be like that. A}
2. Class implementation issues with interfaces
1 Public classBImplementsa{2 3 Public voidfunction1 () {}4 5 Public voidFunction2 () {}//must be public because the Java compiler does not allow lower access permissions in the inheritance relationship. 6 7 //You must implement all the methods in interface a at the same time, or compile an error. This is different from the abstract base class8 9 //You cannot create an object at most if you are not implementing an abstract method entirely in an abstract base class. Ten One}3. Intersection of interface and abstract base class (one-sided summary)
PackageJavaproject;Interfacea{Static voidTest1 () {System.out.println ("interface A");//The static method in the interface must have a function body! voidtest2 ();} Public Abstract classBImplementsa{ Public Static voidTest1 () {System.out.println ("Class B"));} Public Abstract voidtest2 (); //The method that does not need to be implemented is defined as abstract Public Static voidMain (String[]args) {b.test1 (); A.test1 (); //new B ();//Error }}4. Overriding overloads and implementations of the derived classes, base classes, interface methods
The following is an example of "thinking in Java" Forth Edition P181
Packageinterfaces;Interfacei1{voidf ();}Interfacei2{voidFinti);}InterfaceG35intf ();}classc{ Public intF () {return1;}}classC2Implementsi1,i2{ Public voidf () {} Public intFinti) {return1;}}classC3extendsCImplementsi2{ Public intFinti) {return1;}}classC4extendsCImplementsi3{//identical. No problem: Public intF () {return1;}}//!class C5 extends C implements i1{}//!interface I4 extends i1,i3{}
Uncomment the last two lines to compile an error, this example overrides, implementations and overloads are scrambled together, and the overloaded method is not distinguished by the return value type alone
Using the same method names in the different interfaces that you intend to combine often results in confusing code readability and should be avoided as much as possible.
Implementation problems of interfaces in Java