Implementation problems of interfaces in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.