Java Core Technology (VI)--Interface

Source: Internet
Author: User

Behind the post, we will start Java's Common advanced technology learning.
Interface technology, which is mainly used to describe what functions a class has, and does not give a specific implementation of each function. A class can implement (implement) one or more interfaces and use objects that implement the corresponding interface at any time where the interface is needed.

In this article, we will learn more about interfaces from the following major aspects

    • Interface
    • Object cloning
    • Interfaces and callbacks

In addition, there are frequently used comparable and comparator interfaces.

1. Interface

Interfaces are not classes, but rather a set of requirements descriptions for classes that are defined in accordance with the uniform format of the interface description.
Now let's take a look at the code for the comparable interface

publicinterface Comparable{    int compareTo(Object other);}

That is, any class that implements the comparable interface needs to contain the CompareTo method, and the parameter of the method must be an object.
(1) In fact, now the comparable interface has been improved into a generic type

publicinterface Comparable<T>{    int compareTo(T other);}

(2) All methods in an interface are automatically public, so when declaring a method in an interface, you no longer have to provide the keyword public.
(3) At the x.compareTo(y) time of invocation, when x is less than Y, a negative number is returned, equal to 0, otherwise a positive number is returned
(4) The interface can contain several methods, you can define constants, but must not contain the instance domain, nor can implement methods in the interface. The task that provides the instance domain and method implementation should be done by the class that implements the interface. In addition, the fields in the interface are automatically set to public static final.

Then, if you want to sort the employee object array using the Sort method of the arrays class, the employee class must implement the comparable interface.
In order for a class to implement an interface, you typically need:

    • Declares the class to implement the given interface (using the keyword implements)
    • Define all the methods in an interface

(1) Declaring a class to implement a given interface requires the use of the keyword implements:

class Employee implements Comparable

(2) Then provide the CompareTo method for the employee class, assuming that you want to compare the employee's salary

publicintcompareTo(Object otherObject){    Employee other = (Employee) otherObject;    return Double.compare(salary, other.salary);}

Note that the CompareTo method is not declared as public in the interface declaration (top 1), but the method must be declared public (after 2) when implementing the interface.

To avoid a type conversion that always occurs with object, you can replace the above implementation with an implementation of the Comparable<Employee> interface

class Employee implements Comparable<Employee>{    publicint compareTo(Employee other)    {        return Double.compare(salary, other.salary);    }    ···}
1.1 Features of the interface

Interface is not a class, especially an excuse cannot be instantiated with the new operator

new Comparable(···); \\Error

However, although you cannot construct an object of an interface, you can declare the variables of the interface

\\OK

And the interface variable must refer to the class object that implements the interface

x = new Employee(···); \\OK
1.2 Interfaces and abstract classes

There is one problem with using abstract classes to represent common properties: each class can only be extended to one class. But each class can implement multiple interfaces.

Java Core Technology (VI)--Interface

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.