Javase Getting Started learning 20:java Object-oriented interface (interface)

Source: Internet
Author: User

a Java interface

Interface (English: interface) is a collection of abstract methods and constant values, which is an abstract type in the Java programming language, and interfaces typically use interface to sound

Ming. A class is a concrete implementation, and an interface defines the specifications that a class of classes need to follow, an interface that does not care about the internal data of these classes, and does not care about these

The implementation details of the methods in the class, which only stipulate that certain methods must be provided in these classes. A class or interface that inherits the interface by inheriting the interface's

Abstract method.

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

The method 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.

Essentially, an interface is a special kind of abstract class that contains only the definitions of constants and methods, without the implementation of variables and methods, and class-defined

The definition interface no longer uses the class keyword, but instead uses the interface keyword.

The interface cannot be instantiated, but it can be implemented. A class that implements an interface must implement all the methods described in the interface, otherwise it must declare

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 interface

The object to implement.

Two declaration of the interface

Interfaces are used to be inherited, implemented, so modifiers are generally recommended to use public or default; You cannot use private and protected.

Decorative interface. The interface keyword is used to declare an interface.

the declaration syntax format for the interface is as follows:

A static constant: The attribute in the interface is a constant, even if the public static final modifier is not added when defined, the system is automatically added.

B Abstract Method: The method in an interface can only be an abstract method, always used, even if the definition does not add the public abstract modifier, the system will automatically

Plus.

So the interface definition format is:

<span style= "FONT-SIZE:18PX;" >[modifier] Interface Interface Name [extends other interface name] {         //Declare static constant         //Declare abstract method}</span>

Here is a simple example of an interface declaration.

Nameofinterface.java Source file Code:

<span style= "FONT-SIZE:18PX;" >import java.lang.*;//Introduction Package public interface nameofinterface{       //any type static,final field       //abstract method}</span>

The interface has the following characteristics:

(1) interface can be multiple implementations;

(2) The Declaration property in the interface defaults to public static final, and can only be public static final;

(3) Only abstract method methods can be defined in an interface, and these methods are public only, and they are public only; Each method in an interface is also an implicit

Abstract, the declaration of the same do not need to abstract key sub;

(4) The interface can inherit other interfaces and add new attributes and abstract methods;

(5) A plurality of unrelated classes can implement the same interface;

(6) A class can implement a plurality of unrelated interfaces;

(7) Similar to inheritance, there is polymorphism between the interface and the implementation class.

Instance:

Animal.java Source file Code:

<span style= "FONT-SIZE:18PX;" >interface Animal {    //static constant member public    static Final int. age = 5;    Abstract method member public    void Eat ();    public void travel (); </span>

Implementation of the three interfaces

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 uses implements keyword Real

The current interface. 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:

... implements interface name [, other interfaces, other interfaces ...] ...

Example

Continue to implement the interfaces declared in the above instance:

Mammal.java Source file Code:

<span style= "FONT-SIZE:18PX;" >public class Mammal implements animal{    //Override Animal () method in Eat interface public    void Eat () {System.out.println (" Mammal eats ");    }    Override the Travel () method in the animal interface public    void Travel () {System.out.println ("mammal Travels");    }     Mammal Class-Unique Nooflegs () method public    int Nooflegs () {return 0;    }    public static void Main (String args[]) {Mammal m = new mammal (); M.eat (); M.travel ();}    } </span>
The results of the above example compilation run as follows:


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

(1) When implementing a method of an interface, the class cannot throw a mandatory exception, only in an interface, or in an abstract class that inherits an interface.

(2) class to maintain a consistent method name when overriding a method, and should maintain the same or compatible return value type.

(3) If the class implementing the interface is an 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:

(1) A class can implement more than one interface at a time.

(2) A class can inherit only one class, but it can implement multiple interfaces.

(3) An interface can inherit another interface, which is similar to the inheritance between classes.

Four inheritance of interfaces

An interface can inherit another interface, and the inheritance between classes is more similar. Interface inheritance using the extends keyword, the child interface inherits the parent interface

The method.

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

Sports.java Source file Code:

<span style= "FONT-SIZE:18PX;" >public interface sports{    //abstract method public    void Sethometeam (String name);    public void Setvisitingteam (String name);   </span>

Hockey.java Source file Code:

<span style= "FONT-SIZE:18PX;" >public interface Hockey extends sports{    //hockey Interface Own abstract method public    void homegoalscored ();    public void visitinggoalscored ();    public void Endofperiod (int period);    public void overtimeperiod (int ot);   } </span>
Football.java Source file code:

<span style= "FONT-SIZE:18PX;" >public interface Football extends sports{    //football Interface Own abstract method public    void hometeamscored (int points);    public void visitingteamscored (int points);    public void Endofquarter (int quarter);   } </span>
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-side

Method. Similarly, a class that implements the football interface needs to implement five methods, two of which are from the sports interface.

Multiple inheritance of five interfaces

In Java, multiple inheritance of a class is illegal, but the interface allows multiple inheritance. In multiple inheritance of interfaces, the extends keyword only needs to use a

followed by the inherited interface. As shown below:

<span style= "FONT-SIZE:18PX;" >public interface Hockey extends Sports, event{       }</span>

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 approach.

Six identity interface

The most common inherited interface is an interface that does not contain any methods. The identity interface is an interface that does not have any methods and properties, it merely indicates that its class belongs to the

A specific type for other code to test is allowed to do some things.

Identity 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 MouseListener interface in the Java.awt.event package inherits the Java.util.EventListener interface definition:

<span style= "FONT-SIZE:18PX;" >package Java.util;public Interface eventlistener{     //class body details}</span>

Interfaces that do not have any methods are called labeled interfaces. The tag interface is mainly used for the following two purposes:

(1) Establish a common parent interface

As with the EventListener interface, which is a Java API that is extended by dozens of other interfaces, you can use a markup interface to create a set of interfaces for the parent

Interface. For example, when an interface inherits the EventListener interface, the Java Virtual Machine (JVM) knows that the interface will be used for the agent of an event

Case.
(2) Add a data type to a class

This situation is the initial purpose of the markup interface, 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.

Class Seven inherits the parent class and implements the interface

If you want to inherit the parent class, the inherited parent class must precede the implementation of the interface. Inherit the syntax of the parent class implementation interface:

<span style= "FONT-SIZE:18PX;" >[modifier] class class name extends parent class implements interface 1, interface 2 ... {The           Class body part///If you inherit an abstract class, you need to implement an abstract method of inheritance, to implement an abstract method in an interface. }</span>

Instance:

Demo.java Source file Code:

<span style= "FONT-SIZE:18PX;" >public class demo{public    static void Main (string[] args) {Dog D1 = new Dog ();d 1.eat ();d 1.run ();    } Class animal{public String name;public int age;public void Eat () {System.out.println ("Animal has the ability to eat");}} Interface Runner {public void Run ();} Class Dog extends Animal implements runner{public void Eat () {System.out.println ("Dog has the ability to eat Bones");} public void Run () {System.out.println ("Dog has the ability to run");}} </span>

Operation Result:


Summary A interface is similar to a class:

(1) An interface can have multiple methods.

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

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

(4) The corresponding bytecode file for the interface must be in the directory structure that matches the package name.

The difference between a B interface and a class:

(1) An interface cannot be used to instantiate an object.

(2) The interface has no construction method.

(3) All methods in an interface must be abstract methods.

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

(5) The interface is not inherited by the class, but is implemented by the class.

(6) The interface supports multiple inheritance, and the class only supports single inheritance.


Javase Getting Started learning 20:java Object-oriented interface (interface)

Related Article

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.