Learn Java for Android Development Second Edition notes (vi)-Interface

Source: Internet
Author: User
Tags instance method

Interfacejava uses the interface keyword to introduce a type that is not specifically implemented. Declaring interfacesinterface generally starts with a capital letter and ends with a able word, as in the following example:
Interface Drawable{int RED = 1;//For simplicity, integer constants is used. These constants areint GREEN = 2; Not this descriptive, as you'll see.int BLUE = 3;int BLACK = 4;void Draw (int color);}
A few notes on the interface statement:
    • If you need to be accessed by another package, you can add public before interface, otherwise the default can only be accessed by other classes within the package.
    • There is no need to add abstract before interface, because it is actually already abstract.
    • The property of the member variable inside the interface is public final static, and it is not necessary to display the three properties again.
    • The member variables inside the interface must show initialization.
    • The member function property inside the interface is public abstract, and it is not necessary to display the two properties again.
    • The member function inside the interface cannot be declared static because the member function in interface is instance method.
    • A interface without a member is called Marker interface
The Drawable interface declares what to do, but does not define what to do. There are classes that implement the interface to define what to do. Implementing Interfaces
Class Point implements drawable{@Overridepublic void draw (int color) {System.out.println ("point drawn at" + toString () + " In color + color);}}
The above implementation interface to note two places, one is the draw function to add public, because the interface is defined in the public, the other is added @override. You can assign a reference to an object to a variable of the interface type implemented in the object, such as the following example:
public static void Main (string[] args) {drawable[] drawables = new drawable[] {new Point (Ten), New Circle (10, 20, 30)} ; for (int i = 0; i < drawables.length; i++) Drawables[i].draw (drawable.red);}
The above method also realizes polymorphism. If an object implements two interfaces, the view of the two interfaces to which the object is converted can be converted to each other, as in the following example:
public static void Main (string[] args) {drawable[] drawables = new drawable[] {new Point (Ten), New Circle (10, 20, 30)}; for (int i = 0; i < drawables.length; i++) Drawables[i].draw (drawable.red); fillable[] Fillables = new Fillable[drawables.length];for (int i = 0; i < drawables.length; i++) {fillables[i] = (Fillab Le) Drawables[i];fillables[i].fill (Fillable.green);}}
If a class implements multiple interfaces at the same time, it is possible to have a naming conflict, such as a function with the same name and parameters in two interfaces. Extending Interfacesinterface can be inherited, such as the following example:
Interface Colors{int RED = 1;int GREEN = 2;int BLUE = 3;int BLACK = 4;} Interface Drawable extends colors{void draw (int color);} Interface Fillable extends colors{void fill (int color);}
Why use Interfaces? Java provides the interface interface not only because it does not provide multiple inheritance functions, but rather a more important realization of the idea: separating interfaces and implementations, interface-oriented programming(Interfaces can be implemented by the interface or abstract class). Whenever possible, try to specify the interface in the code instead of the specific class so that the code can adapt to changes, especially when using the Java Collections Framework. Here is an example of inflexible:
arraylist<string> ArrayList = new arraylist<string> (); void dump (arraylist<string> ArrayList) {// Suitable code to dump out the arrayList}
The ArrayList class is specific to many places in the example above, and if one day you want to change the use of a type of list such as LinkedList, then you need to modify a lot of places. The above example can be changed to rely on an interface rather than a specific implementation class, for example:
list<string> list = new arraylist<string> (), void dump (list<string> list) {//suitable code to dump th E list}
Change to the above mode, if you want to swap with LinkedList, then only need to modify a place to be. Some summaries of interface and abstract class:
    • These two are the two abstract types provided by Java and cannot be instantiated directly
    • Interface does not depend on any class, and can have any class to implement.
    • The Abstract class can be used in conjunction with 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.