Java foundation Series - Interface (features, uses and advantages)

com.test1 Package; 

/ ** 
 * interfaces utilized 
 * / 
public class test1 { 

    public static void main (String [] args) { 
        // create the Compute 
        the Compute Compute the Compute new new = (); 
        // Create Camera 
        Camera Camera Camera = new new (); 
        // create Phone 
        Phone Phone = new new Phone (); 
        // call 
        compute.useUsb (camera); 
        compute.useUsb (Phone); 

        / * show 
            me a camera and began to work 
            I was camera stopped working 
            I phone and began to work 
            my phone stopped working 
        * * / 
    } 
} 

/ ** 
 * define a USB port  
 * /
interface Usb { 
    // declares two interface methods 

    // start working 
    public void Start (); 

    // stopped 
    public void STOP (); 
} 

/ ** 
 * define a Computers, Usb using the interface 
 * / 
class the Compute { 
    void useUsb public (Usb USB) { 
        usb.start (); 
        usb.stop (); 
    } 
} 

/ ** 
 * define a camera class that implements the interface Usb 
 * Note: a class implements an interface, this interface should be implemented in the All methods 
 * / 
class the implements camera Usb { 

    @Override 
    public void start () { 
        System.out.println ( "I am a camera, began to work"); 
    } 

    @Override 
    public void STOP () { 
        System.out.println ( " I am a camera, stopped working "); 
    } 
} 

/ ** 
 * define a collection class 
 * /
Phone Usb {the implements class 

    @Override 
    public void Start () { 
        System.out.println ( "I am a cell phone, began to work"); 
    } 

    @Override 
    public void STOP () { 
        System.out.println ( "I am a cell phone, stopped working "); 
    } 
} 

/ * 
summarized the significance of JAVA interfaces exist four points: 

1, or importance: in the Java language, abstract class and interface supports two mechanisms abstract class definition. It is the existence of these two mechanisms, it gives a powerful object-oriented Java capability. 
2, simple and normative: if a project is relatively large, then there is a need to be able to sort out all the business architect to define some of the main interfaces that developers not only tell you need to implement those services, but will also naming restrictions live (to prevent some developers just can not be named lead other programmers understand).
3, maintenance, scalability: for example, you have to do a sketchpad program, of which there are a panel class, is responsible for drawing function, so then you define the class. But in the near future, you suddenly find that this class can not satisfy you, then you have to redesign the class, even worse is you may have to give up this kind, other places may quote him, so modify a lot of trouble. If you start to define an interface, the drawing function on the interface, and then implement this interface when defining a class, then you just use this interface to reference classes that implement it on the line, then later want to change just a reference to another class only, so that to achieve the maintenance, expansion of convenience. 
4, security, rigor: Interface software is an important means to achieve loose coupling, which describes all of the external service system, without involving any specific implementation details. This relatively safe, close some of the (general software service providers to consider more). 

So what is the interface it?
The interface is the ability to 
1: Interface naming conventions and different types. If the modifier is public. The interface can be seen, the modifier is omitted if the interface can only be seen in this package in the project 
2: interfaces may define a constant, a variable can not be defined, the properties of the interface with public static final automatically modified, i.e. interface static attributes are global constants, constant interface must be specified when defining the initial value 
3: all interface methods abstract methods. The method automatically interfaces with public abstract modification. I.e., only the global interface abstract method, 
4: as abstract classes and interfaces can not be instantiated, the constructor does not have dog interface 
5: extends through the inheritance relationship can be achieved between the interface, an interface can inherit multiple interfaces. But the interface can not inherit class 
6: all methods of the interface implementation class must implement the interface, or you must be defined as abstract class 
7: A class can have only one direct parent, but can implement multiple interfaces implements, inherited his father when Ray before 1200 class implements multiple interfaces while, extends keyword must be in the implements keyword

Notes interface: 
01. interface can not be instantiated, because the interface is an abstract class higher level of abstraction than the type 
02 if a class implements an interface, you must override all the methods of the interface 
03. Interface not have structure, but the abstract class can have 
abstract methods 04. All methods are public interface of 
all the fields is 05. interface must be public static constant 
06. the interface itself is a data type 
07. Interface specification only defines the classes that implement it, to ensure the implementation class and the corresponding interface method signatures consistent method. 
08. multiple inheritance can be achieved through the interface
09. a pretext best only define a means of preventing contamination of the interface 

characteristics of the interface in java: 
1. interface, is always modified public 
2. no constructor interface, the interface can not be instantiated objects 
3. only method declared in the interface, there is no method body 
4. Interface only constant, if you define a variable, will default at compile time to add "public static final" 
5. the method defined in the interface implementation class needs to implement, if realized All methods can not be achieved in the interface class is implemented as an abstract class is defined class needs 
6. static methods can not be overridden by subclasses (coverage), so the interface variable declared static methods 
7. multiple inheritance interface 
* /

  

Guess you like

Origin www.cnblogs.com/smartsmile/p/11535804.html