Java Interface interface

Source: Internet
Author: User
Tags define abstract

Interface

1. Multiple unrelated classes can implement the same interface

2, a class can implement a number of unrelated interfaces

3. Similar to inheritance, there is polymorphism between interface and implementation class

4. Define the syntax format for Java classes

< modifier> class < name>[extends< superclass>][implements< interface>[, interface]*]{

<declarations>*

}

  

Report:

1, interface (interface) is the combination of the abstract method and the definition of the solid value.

2, in essence, the interface is a special kind of abstract class, this kind of abstract class contains only the solid and the definition of the method, but not the implementation of variables and methods.

3, the interface definition example:

 Public Interface runner{    publicstaticfinalint id = 1;      Public void STRRT ();      Public void run ();      Public void stop ();}

Interface Features:

1, interface can achieve multiple inheritance

2, the properties of the interface declaration default to public static final, or only public static final;

3, the interface can only define abstract methods, and these methods default to public, can only be public;

4, the interface can inherit other interfaces, and add new attributes and abstract methods;

The use of interfaces and the implementation of interfaces are polymorphic:

 Public classtestinterface{ Public Static voidMain (String args[]) {Singer student=NewStudent ("Studentname");        Student.sing ();                Student.sleep (); Teacher Teacher=NewTeacher ("TeacherName");        Teacher.painter ();                Teacher.eat (); Painter Painter=(Painter) teacher;        Painter.painter ();        Painter.eat (); //The following is a partial implementation of polymorphismTestInterface T1 =NewTestInterface ();        T1.F (student);    T1.F (teacher); }     Public voidF (Singer s) {s.sing (); }}Interfacesinger{ Public voidSing ();  Public voidsleep ();}Interfacepainter{ Public voidpainter ();  Public voideat ();}classStudentImplementssinger{PrivateString name;  PublicStudent (String name) { This. Name =name; }     PublicString GetName () {returnname; }     Public voidStudy () {System.out.println ("Studying ..."); }     Public voidSing () {System.out.println ("Study is singing"); }     Public voidsleep () {System.out.println ("Study is Sleeping"); }}classTeacherImplementssinger,painter{PrivateString name;  PublicTeacher (String name) { This. Name =name; }     PublicString GetName () {returnname; }     Public voidSing () {System.out.println ("Teacher is singing"); }     Public voidsleep () {System.out.println ("Teacher is sleeping"); }     Public voidpainter () {System.out.println ("Teacher is paintering"); }     Public voideat () {System.out.println ("Teacher is eating"); }}

Operation Result:

Memory Analysis Diagram:

Java Interface interface

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.