What is the interface in java

First, what is the interface

Interface is a specification similar to the above hardware interfaces, PCI specification slot on the computer motherboard is similar to Java interfaces, as long as the PCI interface card is to follow, but what brand can be inserted into a PCI slot. So the interface is a specification. Interface is to explain some of the features of a thing provided externally. We can also use the interface to achieve multi-state function, but the interface also makes up for the weakness of Java single inheritance, that class can implement multiple interfaces.

We use the interface keyword defines an interface, commonly used method or interface declaration constants, interface methods can only be a declaration, not a specific implementation, and this is an abstract class is not the same. Interface is a higher level of abstraction. Interface Definition Format is

{public interface Interface Name

// You can define constants

// method declaration only method, but is public.

public void method name ();

...

}

Class to implement the interface, only need to use the implements keyword class must implement all of the interface methods to achieve

public class implements the interface implementation class name {

// implementation of the interface method

}

Second, the application interface

Define an interface very simple, in fact, of course, to design a good interface is not very simple, you have to want what is good this interface constants and methods. But the technology is very simple, the following sample code:

// definition method interface 
public  interface MyInterface {
   // definition of constants used by the program interface, the interface can only have constant. 
  public  static  Final  Double . price = 1450.00 ;
   public  static  Final  int counter =. 5 ;
   // interface all the methods are not methods thereof. 
  public  void the Add ( int X, int Y);
   public  void Volume ( int X, int Y, int Z); 
}

 

There is no difficulty implementing the interface, as follows:

                           //实现     接口
public class MyImple implements Myinterface {
    @Override
        public void add(int x, int y) {
    }
    @Override
        public void volume(int x, int y, int z) {
    }
}

 

A class can implement multiple interfaces, because java is single inheritance, interfaces can make up for this. We can then define an interface

public interface MyInterface2 {
     public void countpp();
}

 

Modify the above implementation class to implement multiple interfaces, commas may be used, of course, all of the methods of the interface to be implemented.

                           //实现       接口1,接口2
public class MyImple implements Myinterface ,MyInterface2{
    @Override
    public void add(int x, int y) {
    }
    @Override
    public void volume(int x, int y, int z) {
    }
    @Override
    public void countpp() {
    }
}

 

Third, interfaces and abstract classes What is the difference

abstract class and interface are two mechanisms were supported by the Java language for the definition of abstract class, abstract class and interface between the great similarity in terms of support for the abstract class definition, even interchangeable, so many developers when an abstract class definition is relatively arbitrary choice for the abstract class and interface. In fact, there are still a lot of difference between the two

1, which can have a non-abstract class abstract methods. But the interface can have only abstract methods in the interface embodies a kind of norm, embodied in the abstract class is a template design.

2, the interface (interface) is a variant of the abstract class. In the interface, all the methods are abstract. Multiple inheritance can be obtained by implementing this interface. All interface methods are abstract, without a program body. The interface can only define static final member variables. And subclasses to achieve a similar interface, not inherit behavior from the interface definition in addition to the realization. When implementing special class interface, which the definition of (the procedure of giving) all such interface. Then it can implement any of the methods of the interface class call interface of the image. Because abstract class, which allows the use of interface name as the reference variable type. The usual dynamic binding will take effect. Reference can be converted to interface type, or conversion from interface type, instanceof operator can be used to determine whether an object's class implements the interface

3, the interface can not define a static method in abstract class can. Interfaces in static variables are all constants, the abstract class can have a normal variable.

4. Interface constructors and not in initialization block, there may be an abstract class.

5. A class can implement multiple interfaces, but only inherit an abstract class.

 

Guess you like

Origin www.cnblogs.com/weibanggang/p/11184674.html
Recommended