An interesting java interface and an interesting java Interface

Source: Internet
Author: User

An interesting java interface and an interesting java Interface
The functions of interfaces are not easy to understand for many new users. I will give you an example.
The interface is just a specification, so the methods in it are empty.
If I open a pet grain store and declare that all pets can come to buy food here, this is equivalent to an interface,

Public interface PetRestaurant {
Public void buy ();
}

When a dog sees it and knows that he is a pet, it implements this interface.
Public class DogPet implements PetRestaurant {
@ Override
Public void buy (){
System. out. println ("I'm a dog, I want to buy dog food ");
}
}
When a cat sees it, he knows that he is also a pet, so he also implements this interface.
Public class CatPet implements PetRestaurant {
@ Override
Public void buy (){
System. out. println ("I'm a cat, I want to buy cat food ");
}

}
Before Dogs and Cats came to my shop, I didn't know what they really were, but when they came to my shop, I knew they wanted cat food and dog food. Because they all implement this interface, you can buy it. The following class is equivalent to a customer reception class, that is, the shop assistant. He receives all the animals that implement the interface of my pet shop and transmits them to a pet of the PetRestaurant type. Note that this PetRestaurant is an interface.
Public class test {
Public void buy (PetRestaurant pet)
{
Pet. buy ();
}
}

Okay, this time my boss appears. I can sell them food, which is equivalent to testing.
Public class Tests {
Public static void main (String [] args ){
PetRestaurant dog = new DogPet (); // instantiate a dog, which is equivalent to instantiating a dog customer
PetRestaurant cat = new CatPet (); // instantiate a cat, which is equivalent to instantiating a cat customer

Test t = new test (); // instantiate a store Administrator
T. buy (cat); // give the cat to the store administrator.
T. buy (dog); // give the dog to the store administrator.
}

}
The result is
I'm a cat. I want to buy cat food.
I'm a dog. I want to buy a bitch.

You know, throughout the whole process, I don't know whether it's a cat, a dog, or anything else. If I have a store employee, I handed over all the unknown animals to the store staff, who knew how to sell them, because these dogs and cats all implemented the interfaces of my pet shop, the store Administrator is responsible for receiving all the animals that have implemented this interface. This is a benefit. If I have a pig tomorrow, as long as it implements my interface, I just handed it over to the store staff for processing. I don't need to change this shop staff, the shopkeeper just needs to instantiate this animal.
You think, if there is no interface, what should you do? Come to a cat, I want to create a cat, and instantiate it. Come to a dog, I want to create a dog, and I also want to instantiate it, it would be quite troublesome to have a dedicated store to receive the reception.

Author: meiyunluo
Link: http://www.zhihu.com/question/20111251/answer/14760353
Source: zhihu
Copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

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.