Java object-oriented -006-interface

Source: Internet
Author: User

One, Java interface

An interface is an abstract type in Java, a collection of abstract methods, usually declared as interface. A class inherits the abstract method of an interface by inheriting the interface.

Interfaces are not classes, and the way they are written is similar to classes, but they belong to different concepts. class describes the properties and methods of an object. The interface contains the methods to be implemented by the class .

unless the class that implements the interface is an abstract class, the class defines all the methods in the interface

interfaces cannot be instantiated, but can be implemented. A class that implements an interface must implement all the methods described in the interface, otherwise it must be declared as an abstract class

In addition, in Java, the interface type can be used to declare a variable, they can be a null pointer, or be bound to an object implemented in this interface.

Two, the interface and the similar point of the class:

An interface can have multiple methods

The interface name is saved in a file that ends in. java, and the filename uses the interface name.

The bytecode file for the interface is saved in a file that ends in a. class.

The corresponding bytecode file for the interface must match the package name in the directory structure.

Three, the difference between the interface and the class :

An interface cannot be used to instantiate an object.

Interface has no constructor method.

All methods in an interface must be abstract methods.

An interface cannot contain member variables, except static and final variables.

Interfaces are not inherited by classes, but are implemented by classes.

The interface supports multiple inheritance.

Four, interface characteristics

Each method in the interface is implicitly abstract, and the methods in the interface are implicitly specified as public abstract (only public abstract, and other modifiers will be error-marked).

The interface can contain variables, but the variables in the interface are implicitly specified as public static final variables (and can only be public, with the private modifier reporting compilation errors).

The method in the interface is not implemented in the interface, only the class implementing the interface can implement the method in the interface.

V. The differences between abstract classes and interfaces

1. A method in an abstract class can have a method body, which is a specific function that can implement a method, but the method in an interface is not.

2. member variables in an abstract class can be of various types, whereas member variables in an interface are only public static final types .

3. the interface cannot contain static code blocks and static methods (Methods that are modified with static), whereas abstract classes can have static blocks of code and static methods.

4. A class can inherit only one abstract class, while a class may implement multiple interfaces .

VI. Declaration and implementation of interfaces

The declaration syntax format for the interface is as follows:

/* [Visibility] Interface interface Name [extends other class name] {    //DECLARE variable    //abstract method}* /

The interface keyword is used to declare an interface. Here is a simple example of an interface declaration.

/*file name: Nameofinterface.java*/Importjava.lang.*;//Introduction Package Public Interfacenameofinterface{//any type final, static field//Abstract Methods}//the interface has the following characteristics://interfaces are implicitly abstract, and it is not necessary to use the abstract keyword when declaring an interface. //each method in the interface is implicitly abstract, and the declaration does not require the abstract keyword. //The methods in the interface are public. 
// Animal.java file Code: /*  */interface  Animal    {publicvoid  eat ()    ;  Public void Travel ();}

Implementation of the interface

When a class implements an interface, the class implements all the methods in the interface. Otherwise, the class must be declared as an abstract class.

class implements the interface using the Implements keyword. In a class declaration, the Implements keyword is placed after the class declaration

To implement the syntax of an interface, you can use this formula:

/**/interface  Animal {    publicvoid  eat (    );  Public void Travel ();}

//... implements interface name [, other interface name, other interface name ..., ...] .../*file name: Mammalint.java*/ Public classMammalintImplementsanimal{@Override Public voideat () {System.out.println ("Mammal Eats"); } @Override Public voidTravel () {System.out.println ("Mammal Travels"); }     Public intNooflegs () {return0; }     Public Static voidMain (string[] args) {mammalint mammalint=NewMammalint ();        Mammalint.eat ();    Mammalint.travel (); }    //mammal Eats//Mammal Travels}

When overriding a method declared in an interface, you need to be aware of the following rules:

// class cannot throw an exception when implementing the method of an interface, it can only be thrown in an interface, or in an abstract class that inherits an interface . // class must maintain consistent method names when overriding, and should maintain the same or compatible return value types // If you implement the class of an interface abstract class, then there is no need to implement the method of the interface

There are some rules to be aware of when implementing an interface:

// A class can implement more than one interface at a time.  // A class can inherit only one class, but can implement multiple interfaces.  // an interface can inherit another interface, which is similar to the inheritance between classes. 

Vii. Inheritance of interfaces

An interface can inherit another interface, and the inheritance between classes is more similar. The inheritance of an interface uses the extends keyword, which inherits the method of the parent interface.

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

//file name: Sports.java Public Interfacesports{ Public voidSethometeam (String name);  Public voidSetvisitingteam (String name);}//file name: Football.java Public InterfaceFootballextendssports{ Public voidHometeamscored (intpoints);  Public voidVisitingteamscored (intpoints);  Public voidEndofquarter (intquarter);}//file name: Hockey.java Public InterfaceHockeyextendssports{ Public voidhomegoalscored ();  Public voidvisitinggoalscored ();  Public voidEndofperiod (intperiod);  Public voidOvertimeperiod (intot);}//The hockey interface itself declares four methods, inheriting two methods from the sports interface, so that the class implementing the Hockey interface needs to implement six methods. //Similarly, a class that implements the football interface needs to implement five methods, two of which are from the sports interface. 

Multiple inheritance of interfaces

In Java, multiple inheritance of a class is illegal, but the interface allows multiple inheritance.

In multiple inheritance of interfaces, the extends keyword needs to be used only once, followed by an inherited interface. As shown below:

 Public Interface extends Sports, Event

The above program fragment is a legally defined sub-interface, and unlike a class, the interface allows multiple inheritance, while sports and Event may define or inherit the same method

The most commonly used inheritance interface for tag interfaces is an interface that does not contain any methods.

The markup interface is an interface that does not have any methods and properties. It merely indicates that its class belongs to a particular type, and that other code is allowed to do some things for testing.

Tag interface function: The simple image is to give an object a mark (a stamp), so that the object has some or some of the privileges.

For example: The Java.util.EventListener interface that inherits from the MouseListener interface in the Java.awt.event package is defined as follows:

 Package Java.util;  Public Interface eventlistener{} // interfaces that do not have any methods are called labeled interfaces. The markup interface is mainly used for the following two purposes: // Create a common Parent interface:// as the EventListener interface, which is a Java API extended by dozens of other interfaces, You can use a markup interface to create a parent interface for a set of interfaces. For example: When an interface inherits the EventListener interface, the Java Virtual Machine (JVM) knows that the interface will be used for an event's proxy scenario.  // Add a data type to a class:// This is the initial purpose of the markup interface, and the class that implements the tag interface does not need to define any interface methods (because the markup interface has no method at all). However, the class becomes a type of interface through polymorphism. 

Java object-oriented -006-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.