Talking about Java Interface__java

Source: Internet
Author: User

For Java beginners, there may be such a doubt----we define an interface, but we implement this interface in the class, but also to implement all the methods in the interface, it is not as straightforward to write the implementation method in this class is not more convenient, but also eliminates the definition of interface trouble. So what is the function of the interface in Java?

1. Definition

An interface is composed of a set of related empty methods.

2, the role

Interface is a specification. Because it may have to be implemented by multiple classes, how does the program know they are related? Since not a class to achieve, then there are many places to be useful, we need to unify the standards. Even some programming language (object-) has not called the interface interface, directly called protocol.

The aim of the unified standard is that we all know what it is, but we don't need to know how to do it. For example, comparable to find an interface to compare two objects, then how to compare it.

Numbers have a comparison method of numbers, strings have a comparison method of strings, students (their defined classes) also have their own comparison methods.

Software development in a very important idea is "decoupling", interface and interface implementation is in the dry this thing, set good specifications, let you achieve, efficiency can be high, can be low, the implementation of content can also change over time, but the call relationship is unchanged.

3. Advantages:

(1) The program can be extended. When you modify your specific implementation class, you do not need to modify the class that invokes it; this is the "open for expansion, close to modification" principle that is emphasized in Java programming. Of course, this is also the embodiment of Java polymorphism.

(2) is Java through interface implementation of multiple inheritance. One benefit of inheritance is the reuse of code, which reduces code errors.

4. Living examples

(1) If I open a pet stores, declare that all pets can come to me to buy food, which is equivalent to an interface.

public interface petrestaurant{

public void Buy ();

}

When a dog sees it and knows he's a pet, it's going to implement 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 and knows that he is a pet, so it implements this interface

public class Catpet implements petrestaurant{

@Override

public void Buy () {

System.out.println ("I am a cat, I want to buy cat food");

}

When dogs and cats come to my shop, I don't know what they really are, but when they come to my shop, I know one for cat food and one for dog food. Because they all realize my interface, can buy.

The following class is equivalent to a class that receives customers, namely bartender, who receives all the animals that have realized the interface of my pet store and comes in a petrestaurant type of pet, note that this petrestaurant is an interface

public class test{

public void Buy (Petrestautrant pet) {

Pet.buy ();

}

}

Well, this time my boss showed up, I can sell them food, equivalent to test class

public class tests{

public static void Main (string[] args) {

Test t=new test ();

Petrestaurant dog=new Dogpet ();

Petrestaurant cat=new Catpet ();

T.buy (dog);

T.buy (CAT);

}

}

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.