Java-7.2 Interface

Source: Internet
Author: User

Java-7.2 Interface

In this section, we will discuss interfaces.

We have talked about abstract classes before. He has abstracted the first step, abstracted some methods, and implemented them in the subclass, but he is not completely abstract.

Interfaces are further abstracted. They are all non-implemented methods, so all methods are implemented in the implementation class.

1. Concept

Interface: Like a protocol between a class and a class, you only need to know an interface implemented by a class, you can call the methods in the interface to point to the implementation of this class.

 

2. Features

(1) Use interface Annotation

(2) completely abstract

(3) The attribute field must be public final static (this is automatically converted by the compiler)

(4) The method must be public.

(5) The upward transformation provides a way for the parent class to point to the subclass object, and also enables java to have polymorphism.

(6) It cannot be instantiated.

(7) Multiple implementations of interfaces

 

interface Instrument {int id=0;void Play();}


 

3. Instance

 

Package com. ray. ch07; public class Test {public void tune (Instrument instrument) {instrument. play ();} public void tune (Instrument [] instruments) {for (int I = 0; I <instruments. length; I ++) {tune (instruments [I]) ;}} public static void main (String [] args) {Test test = new Test (); // Instrument instrument = new Instrument (); // errorInstrument wind = new Wind (); Instrument bass = new Bass (); Instrument [] instruments = {wind, bass }; test. tune (instruments); System. out. println (Instrument. id); // id is static} interface Instrument {// private int id = 0; // error // private void Play (); // errorint id = 0; void Play ();} class Wind implements Instrument {@ Overridepublic void Play () {// id = 2; // The final field Instrument. id cannot be assignedSystem. out. println (id); System. out. println (wind play);} class Bass implements Instrument {@ Overridepublic void Play () {System. out. println (bass play );}}


 

Output:

0
Wind play
Bass play
0

 

From the code above, you can see:

1. The interface cannot be new.

2. Methods that subclass must implement interfaces

3. Through upward transformation, the wind and bass defined by the Instrument type can also call the play method and automatically identify the play method to be bound during runtime.

4. Since wind and bass must rewrite play, play must be public, because wind and bass do not inherit Instrument

5. by printing the id in the Instrument, we know that the id must be static, because it can be called directly using the Instrument without the need for new

6. By modifying the id in the implementation class wind, you can see his prompt that the id is final.

7. The multiple implementations of the interface will be described later.

 

Summary: This chapter mainly discusses the concepts and features of interfaces and provides examples to illustrate these features.

 

 

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.