2013年10月21日 星期一

java-interface


inf : http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html

There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a "contract" that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group's code is written. Generally speaking, interfacesare such contracts.
For example, imagine a futuristic society where computer-controlled robotic cars transport passengers through city streets without a human operator. Automobile manufacturers write software (Java, of course) that operates the automobile—stop, start, accelerate, turn left, and so forth. Another industrial group, electronic guidance instrument manufacturers, make computer systems that receive GPS (Global Positioning System) position data and wireless transmission of traffic conditions and use that information to drive the car.
The auto manufacturers must publish an industry-standard interface that spells out in detail what methods can be invoked to make the car move (any car, from any manufacturer). The guidance manufacturers can then write software that invokes the methods described in the interface to command the car. Neither industrial group needs to know how the other group's software is implemented. In fact, each group considers its software highly proprietary and reserves the right to modify it at any time, as long as it continues to adhere to the published interface.




  • An interface is not extended by a class; it is implemented by a class.
  • An interface is a collection of abstract methods.
  • A class implements an interface, thereby inheriting the abstract methods of the interface.
  • An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts.
  • A class describes the attributes and behaviors of an object.
  • An interface contains behaviors that a class implements.
  • An interface is similar to a class in the following ways:
    An interface can contain any number of methods.
    An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
    The bytecode of an interface appears in a .class file.
    Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.
  • However, an interface is different from a class in several ways, including:
    You cannot instantiate an interface.
    An interface does not contain any constructors.
    All of the methods in an interface are abstract.
    An interface cannot contain instance fields.
    The only fields that can appear in an interface must be declared both static and final.

沒有留言:

張貼留言