Some vivid examples of Java Interfaces

Source: Internet
Author: User

You should know that an interface is a contract and has nothing to do with the implementation method.

However, you can customize member variables for classes, even abstract classes. member variables are often related to implementation methods.

This is of little practical significance.

But one thing is that classes expose too many unnecessary or even non-exposing things. In Java. util, most of the data structures are designed as interfaces, abstract classes, and final actual classes.

For example, collection-list
L-abstractcollection
L-abstractlist
L-arraylist

However, for historical reasons, it is designed as a class, such as stack extends vector,
You should know the Data Access Mode of the stack. It can only be lifo, but the vector is a list that can be accessed randomly and can be increased or decreased at will.

Result stack S = new stack ();

Not only can pop/push/peer be added, get, set, remove

If you only use the POP/push/peer method in the istack interface, and then implement it by inheriting the vector method,

Istack S = new mystack ();

In this case, you cannot add, get, set, and remove

A dynamic example

Public interface britishspy {
Public String speak (); // english-speaking British espionage
}

Public interface germanspy {
Public String sprechen (); // German espionage speaks German
}

Public class doubleagent implements britishspy, germanspy {
Public String speak () {return "hello ";}
Public String sprechen () {return "gutentag ";}
}

Public class agency {
Public static void tomi5 (briishspy spy ){
// Of course, I can only speak English and be a British spy.
Spy. Speak ();
// Spy. sprechen (); invisible
}

Public static void ingermany (germanspy spy ){
// Spy. Speak (); invisible
Spy. sprechen ();
}

Public static void main (string [] ARGs ){
Doubleagent da = new doubleagent ();
Engishspy es = (engishspy) Da;
Germanspy GS = (germanspy) Da;
Tomi5 (DA); // MI5 does not know that he is a dual-agent, but only that he is a British
Tomi5 (ES); // safer
// Tomi5 (GS); impossible
Ingerspy (DA); // It is still safe in Germany. The Germans do not know his dual espionage identity, but only know that he is germanspy.
Ingermany (GS );
// Inger.pdf (ES); impossible
}
}

Suppose you only use the class, because it cannot be inherited multiple times, so the speak ()/sprechen () statement is in the same class.

Public abstract class doubleagent extends spy/** (Omitted ...)*/{
Public abstract string speak ();
Public abstract string sprechen ();
}

Public class poordoubleagent {
Public String speak () {return "hello ";}
Public String sprechen () {return "gutentag ";}
}

It's too late. No matter whether you are poordoubleagent A = new poordoubleagent () or doubleagent A = new poordoubleagent (); the whole world knows that he is a dual-agent and will no doubt die wherever he goes.
The preceding example shows "security ".

The interface is only exposed to the other party (such as the situation 5 Method of the agent). It requires sufficient information, and other irrelevant or even harmful information will not be exposed to the other party. Because the interface type is passed to you, except for the actual exceptions of this interface (and the parent interface of this interface, inteface A extends B, c, you know at most that I am an object (not int: P), who are the other surnames, where is the address, where are the parents? Well, you have nothing to do with Brother and Sister ry, we only need to care about the contract we signed (Interface)

Another example of flexibility

Assume that a company has updated n generations, and the logic is extremely complex.
Public Class A extends B/** where B extends C, C extends D and so on ...*/{
Public void Init (){...}
Public void release (){...}
Public String doxxx (){...}
Public String doyyy (){...}
}

This a is inherited or used by many classes, and the doxxx/doyyy method cannot be changed.

Assume that the company is now in a Standardization Organization, and this Organization requires all to provide such methods

String getxxx (); string getyyy ();

Join the interface Standardization Organization as long as members are required to implement
Public interface ibusiness {
String getxxx ();
String getyyy ();
}
This company only needs to rewrite it a little bit.
Public Class A extends B/** where B extends C, C extends D and so on ...*/
Implements ibusiness {
Public String getxxx () {return doxxx ();}
Public String getyyy () {return doyyy ();}

// Reserved
Public void Init (){...}
Public void release (){...}
Public String doxxx (){...}
Public String doyyy (){...}
}

This not only meets the requirements of standardization, but also satisfies the numerous classes inherited from a or used by a (some may be in the user and cannot be changed)

If you do not need an interface, you have two options: Forgetting the ancestor of a number or killing others.

Forget the ancestor of the number:
Your new A must inherit the business of the Standardization Organization. It turns out that a, B, c d... all the code in it must be re-organized into the new A. At the same time, the class that calls or inherits a does not need to be rewritten.

Personal
Let it go, and no one will mention it. We will use the new newa later. As a result, your new customers have met the standards, and your old customers can: <: @: $, and a and newa need to be maintained later
Interface Definition:
Interface fight {
Void fight ();
}

Fat and thin to implement this interface:
Class fatfat implements fight {
Public void fight (){
System. Out. println ("It hurts to beat fatfat! ");
}
}

Class thinthin implements fight {
Public void fight (){
System. Out. println ("thinthin does not hurt at all !! Haha. ");
}
}

Then you may use the fatfat and thinthin objects in the other class, and then execute fight, but you may not know the specific class object at runtime, thanks to all of them for implementing the fight interface, you can transform up and then generate the desired behavior through the running polymorphism.

Fight A = new fatfat ();
Fight B = new thinthin ();
A. Fight ();
B. Fight ();
In this way, different actions are executed.

Or if you have a method
F (fight I ){
I. Fight ();
}

If C is one of the classes implementing the fight interface, you can use this method as follows:
F (C );
You don't need to know what object C is (whether fatfat or thinthin), you can get the fight action you want.

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.