<a href= "http://www.goodprogrammer.org/"
target=
"blank"
>android培训</a>------我的java笔记,期待与您交流!
In an abstract class, you can include one or more abstract methods, but in an interface (interface), all methods must be abstract and cannot have a method body, which is more "abstract" than an abstract class.
Interfaces are declared by using the interface keyword, which can be seen as a special abstract class that specifies what a class must do, not how it is to be done.
In reality there are also many examples of interfaces, such as the serial computer hard disk, Serial ATA Commission designated the Serial ATA 2.0 specification, which is the interface. The Serial ATA Committee is not responsible for the production of hard drives, but specifies generic specifications.
Seagate, Hitachi, Samsung and other manufacturers will be in accordance with the specifications of the production of the interface of the hard disk, these hard drives can be generalized, if you are using a 160G Hitachi serial drive, now to upgrade, you can buy a 320G of the Seagate Serial drive, installation can continue to use.
The following code can simulate the Serial ATA board to define the following serial drive interfaces:
Serial hard drive interface public interface satahdd{//cable number public static final int connect_line=4;//write data public void WriteData (String data) ;//Read Data public String readdata ();}
Serial hard drive Interface public interface satahdd{//cable number public static final int connect_line=4; Write Data public void WriteData (String data); Read Data public String readdata ();}
Note: The member variables declared in the interface are public static final by default and must be displayed for initialization. Thus, these modifiers can be omitted at constant declarations.
Interfaces are a collection of constants and abstract methods that appear to be similar to abstract classes at this time. Indeed, interfaces are evolved from abstract classes, so that, in addition to special provisions, interfaces enjoy the same "treatment" as classes. For example, a source program can define multiple classes or interfaces, but there can be at most one public class or interface, and if so, the source file must take the same name as the public's class and interface. As with the class's inheritance format, interfaces can also inherit from one another, and the subinterfaces can inherit constants and abstract methods from the parent interface and add new abstract methods.
But the interface has its own characteristics, summarized as follows.
1) Only abstract methods can be defined in the interface, which are public abstract by default, and thus can be omitted when declaring a method. It is illegal to attempt to define instance variables, non-abstract instance methods, and static methods in an interface. For example:
Public interface satahdd{//Number of connection lines public int connectline;//Compile error, Connectline is considered static constant, must be explicitly initialized//write data protected void WriteData (String data); Compile error, must be public type//read data public static String ReadData () {//Compile error, interface cannot contain static method return "data";//Compile error, interface can only contain abstract methods,}}
public interface satahdd{//number of connections public int connectline;//Compile error, Connectline is considered static constant, must be explicitly initialized//write data protected V OID WriteData (String data); Compile error, must be public type//read data public static String ReadData () {//Compile error, interface cannot contain static method return "data";//Compile error, interface can only contain abstract methods, }}
3) There is no construction method in the interface and cannot be instantiated.
4) An interface does not implement another interface, but can inherit multiple other interfaces. The multiple inheritance features of the interface compensate for the single inheritance of the class. For example:
Serial hard drive interface public interface SATAHDD extends a,b{//number of connectors public static final int connect_line = 4; Write Data public void WriteData (String data); Read Data public String readdata ();} Interface a{public void A (); Interface b{public void B ();}
Why use Interfaces
In large-scale project development, you might need to insert a class from the middle of the inheritance chain so that its subclasses have some functionality without affecting their parent class. For example, E,a, C-A, B, a, D, is an ancestor class, and if you need to add some common functionality to the C, D, and E classes, the simplest way is to have the class C inherit another class. But here's the problem, Java is a single-inheritance language, and you can't let C inherit another parent class, just move to the top of the inheritance chain and let a inherit a parent class. In this way, the modification of C, D, E, affects the entire succession chain, does not have the pluggable design.
The interface is the guarantee of the pluggable nature. Any class in an inheritance chain can implement an interface that affects all subclasses of this class, but does not affect any of the parent classes of this class. This class will have to implement the methods specified by this interface, and subclasses can automatically inherit these methods from this class, when these subclasses have the ability to insert.
We are not concerned with a specific class, but whether the class implements the interface we need.
interface provides the correlation and the pluggable on the method call, the larger the scale of the software system, the longer the life cycle, the interface makes the software system flexible and extensible, the pluggable aspect is guaranteed.
Interface plays an important role in object-oriented Java programming. In fact, one of the most important tasks in the design phase is to design the interface of each part and then form the basic framework of the program through the combination of interfaces.
Use of interfaces
The use of interfaces is somewhat different from the use of classes. Where a class is needed, an instance of the class is built using the new keyword directly, but the interface cannot be used because the interface cannot use the new keyword directly to build an instance.
The interface must implement (implements) its abstract method through a class, and then instantiate the class. The keyword for the class implementation interface is implements.
If a class cannot implement all the abstract methods of the interface, then the class must be defined as an abstract method.
Creating an instance of an interface is not allowed, but it allows you to define a reference variable for the interface type that points to an instance of the class implementing the interface.
A class can inherit only one parent class, but it implements multiple interfaces.
The format for implementing the interface is as follows:
Modifier class class name extends parent class implements multiple interfaces {
Implementation method
}
Take a look at the following example:
import static java.lang.system.*;p ublic class demo{ public static Void main (String[] args) { SataHdd sh1=new SEAGATEHDD (); //initialize Seagate HDD satahdd sh2=new samsunghdd (); / /Initialize Samsung HDD }}//serial hard Drive interface interface satahdd{ //number of cables public static final int connect_line=4; //Writing Data public void writedata (string data); //Read Data Public string readdata ();} Repair HDD interface interface fixhdd{ // repair Address String address = "Haidian District, Beijing"; // started maintenance boolean dofix (); Seagate HDD class seagatehdd implements satahdd, fixhdd{ //Seagate HDD ReadFetch Data public string readdata () { return "Data"; } //Seagate HDD Writing data public void writedata (string data) { out.println ( "Write success"); } // repair Seagate HDD public Boolean dofix () { return true; }}//Samsung HDD class samsunghdd implements satahdd{ //Samsung HDD Read Data public string readdata () { return "data "; } //Samsung HDD Write Data public void WriteData (string data) { out.println ("Write succeeded"); }}//a poor hard drive, unable to write dataabstract class xxhdd implements satahdd{ //hard Drive Read Data public string readdata () { return "data "; }}
Interface is used as a type
Interface is used as a reference type, any instance of the class that implements the interface can be stored in a variable of that interface type, through which the methods in the interfaces implemented in the class can be accessed, and the Java runtime system dynamically determines which of the methods in the class should be used, in effect invoking the method of the corresponding implementation class.
Examples are as follows:
public class demo{ public void test1 (A a) { a.dosth (); } public static void main (String[] args) { demo d = new demo (); a a = new b (); d.test1 (A); }}interface a { public int dosth ();} Class b implements a { public int dosth () { system.out.println ("Now in b"); return 123; }}
Operation Result:
Now in B
You see that the interface can be used as a type, using the interface as the parameter and return type of the method.
This article from "Day Up" blog, declined reprint!
Good programmer training Camp concept and use of-java interface (interface)