Interfaces in Java

Source: Internet
Author: User

The following is referenced from http://wiki.jikexueyuan.com/project/java/interfaces.html:

An interface is a collection of abstract methods. If a class implements an interface, it is necessary to inherit all the abstract methods in this interface.

The interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class is a member property and behavior that describes an object. An interface contains only the behavior that a class implements.

Unless the class that implements the interface is abstract, all methods in the interface need to be implemented in the class.

Interfaces and classes are very similar in the following ways:

    • An interface can contain any number of methods.
    • An interface .java is written to the file with the extension, and the name of the interface is the same as the file name.
    • The byte code of the interface is in a .class file.
    • The interface is in the package, and the corresponding bytecode file must be under a folder structure that matches the package name.

However, interfaces and classes are different in the following ways:

    • An interface cannot be instantiated.
    • An interface cannot contain a constructor method.
    • All methods in an interface are abstract.
    • An interface cannot contain an instance variable. The only variables that can occur in an interface must be declared both static and final.
    • An interface cannot be inherited by a class, it should be implemented by a class.
    • An interface can inherit multiple interfaces.

First, the Declaration interface

interfaceKeyword is used to declare an interface. Here is a simple example of declaring an interface:

Example:

Examples of interfaces are described below:

/**/import java.lang.*; // Any number of import statements  Public Interface nameofinterface{   //any numberof the final, static   fields//  Any number of abstract method declarations\}

The interface has the following properties:

    • An interface is abstract by default. You do not need to use keywords when declaring an interface abstract .
    • Each method in the interface is also abstract by default, so the abstract keyword is not required.
    • The methods in the interface are public by default.

Example:

/**/interface  Animal {   publicvoid  eat ();    publicvoid Travel ();

Second, the implementation of the interface

When a class implements an interface, it can be assumed that a class is a treaty that agrees to perform various behaviors in the interface. If a class does not implement all the behavior in the interface, the class must be declared abstract.

Class uses implements keywords to implement an interface. This implements keyword is written in the declaration section of the class extends (if any) behind the section.

/*File Name:MammalInt.java*/ Public classMammalintImplementsanimal{ Public voideat () {System.out.println ("Mammal Eats"); }    Public voidTravel () {System.out.println ("Mammal Travels"); }     Public intNooflegs () {return0; }    Public Static voidMain (String args[]) {mammalint m=NewMammalint ();      M.eat ();   M.travel (); }} //this will produce the following result:Mammal Eatsmammal Travels

When you overwrite a method defined in an interface, here are a few rules to follow:

    • Exceptions should not be declared in the implemented method, but should be in the declared interface method or in the subclasses of the interface that declares the method.
    • The method name of the interface method and the return value of the same type or subtype should be included when the method is overwrite.
    • The interface implementation class itself can be abstract, and if it is abstract, the methods in the interface do not need to be fully implemented.

There are several rules when implementing an interface:

    • Classes can implement multiple interfaces at once.
    • A class can inherit only one parent class, but it can implement multiple interfaces.
    • An interface can inherit from another interface, and a class inherits from another class in the same way.

Third, the inheritance of the interface

An interface can inherit another interface, and one class inherits from the same method as another. The extends keyword is used to inherit an interface, and the sub-interface inherits all the methods of the parent interface.

The following sports interfaces are inherited by the hockey and football interfaces.

//Filename:Sports.java Public Interfacesports{ Public voidSethometeam (String name);  Public voidSetvisitingteam (String name);}//Filename:Football.java Public InterfaceFootballextendssports{ Public voidHometeamscored (intpoints);  Public voidVisitingteamscored (intpoints);  Public voidEndofquarter (intquarter);}//Filename:Hockey.java Public InterfaceHockeyextendssports{ Public voidhomegoalscored ();  Public voidvisitinggoalscored ();  Public voidEndofperiod (intperiod);  Public voidOvertimeperiod (intot);}

The hockey interface has four methods, but it inherits two from the sports interface, so a class that implements the hockey interface needs to implement all six methods. Similarly, a class that implements the football needs to define two methods in the three methods and sports interfaces in the football interface.

Iv. Inheritance of multiple interfaces

A Java class can inherit only one parent class and cannot inherit more. However, an interface is not a class, and an interface can inherit multiple parent interfaces.

Once a extends keyword is used, all parent interface declarations need to be separated by commas.

For example, if the hockey interface inherits both the sports and the event interface, it needs to be declared as follows:

 Public Interface extends Sports, Event

Five, the Identity interface

The most common use of an inherited interface is that the parent interface does not contain any methods. For example, the MouseListener interface in the Java.awt.event package inherits the Java.util.EventListener interface, as defined below:

 Package Java.util;  Public Interface eventlistener{}

An internal interface without any method is called tagging interface. The tagging interface has two basic uses:

Create a common parent class: Like the EventListener interface, which inherits many of the other interfaces in the Java API, you can use the tagging interface to create a common parent class in a set of interfaces. For example, when an interface inherits the EventListener interface, the Java Virtual Machine (JVM) knows that this particular interface is used on the event proxy.

To add a data type to a class: A class that implements the tagging interface does not need to define any method (because there is no method in this interface), but this class becomes an interface type through polymorphic attributes.

Test Project: https://github.com/easonjim/5_java_example/tree/master/javabasicstest/test22

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.