Understanding the java-7.2 interface from the beginning

Source: Internet
Author: User

Let's discuss the interface in this section.

Before we had talked about abstract classes, he had taken the first step of abstraction, abstracted some methods, and then implemented them in subclasses, but he was not completely abstract.

And the interface, is the further abstraction, it is completely not implemented in the method, so the method is implemented in the implementation of the class.

1. Concept

Interface: Just like a protocol between a class and a class, you only need to know an interface that is implemented by a class, so that he can point to the implementation of the class by invoking a method inside the interface.


2. Features

(1) Using interface labeling

(2) Complete abstraction

(3) The domain must be public final static (this is the compiler automatically converted)

(4) The method must be public

(5) Upward transformation, providing a way for the parent class to point to the subclass object, while also allowing Java to have polymorphic properties

(6) cannot be instantiated

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


3. Example

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 ();//errorinstrume NT 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 assigneds YSTEM.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 is not new

2. Subclasses must implement the method of the interface

3. By moving up, wind and bass, defined as type instrument, can also call the play method, and the runtime automatically recognizes that the play method should be bound

4. Because wind and bass must rewrite play, play must be public because wind and bass are not inherited instrument

5. By printing the ID inside the instrument, we know that the ID must be static because you can use the instrument call directly without the need for a new

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


Summary: This chapter focuses on the concepts, features, and examples of interfaces.


This chapter is here, thank you.

-----------------------------------

Directory



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Understanding the java-7.2 interface from the beginning

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.