Java Advanced Features: Interface

On completion of abstract classes to learn interface.

What interfaces are?

Interface is not a class, but a set of requirements for the class description. Such as our mouse, U disk all have a unified USB interface. The method of any class that implements a particular interface, the interface must implement this provision, if only to achieve a part of, then the subclass must be declared as an abstract class, and this class as abstract.
Sample code.

public interface Comparable
{
    int compareTo(Object other);
}

//实现
class Employee implements Comparable
{
    public int compareTo(Object otherObject)
    {
        Employee other = (Employee) otherObject;
        return Double.compare(salary, other.salary);
    }
}

With abstract classes, why Interface

java there is no concept of multiple inheritance, in order to achieve multiple inheritance, the introduction of the interface, you can implement multiple interfaces at the same time. Interface can provide most of the benefits of multiple inheritance, but also libido seconds inheritance complexity and inefficiency.

Some hidden characteristics of the interface

  • All interface methods belong to the public, when declaring methods, can not provide the keyword public. But in the method of implementation of the interface, the interface must be declared as public, or the compiler package believed to be visible.
  • The interface is not a class, an interface can not be instantiated, but can be used to declare, and reference implementation of the interface class object.
  • You can use instanceof to check whether an object implements a particular interface.
  • Interface can be extended, the interface is the interface inheritance, and the inheritance of the same class.
  • Examples of the interface can not contain a domain, but may contain, const default public static fianl type.
  • Interface can only constant, only the method, even empty, such as Cloneable (see my other article a clone () method), whether such an interface is used to mark the class can be cloned.

New features in Java8

Before believes interface can not be achieved by any method, now it increased to achieve the two methods. Static methods, and the default method. Static method is modified static method. The default method is the default method for modifying. Because the interface method, we can only focus on a few achieve. Others may not realize it, and use the default method defined in the interface. Second, the default method easily compatible. If the interface directly add new features. Subclass because it will not implement new methods and error. But if there are ways to increase the default implementation, it will be able to achieve compatibility with legacy code.
If a will now interface to a method defined as a default method, then the definition of the same methods in the superclass or another interface, how to do it?

  • Superclass priority. If the super class provides a specific method, and has the same name as the default method for the same type of argument it will be ignored. Superclass priority to ensure compatibility of Java7. Do not defined in the default Object methods, such as equals, toString, never executed because the default method.
  • Interface conflict. If a super-interface provides a default method, another interface provides the same name and the same type of method parameters, you must override this method to resolve the conflict.

Interface and callback

Callback callback is a common programming model. In this design pattern, you can point out the action when a specific event occurs that should be taken. Such as using timers call a method regularly. Because the java object-oriented, so the incoming object. Required to achieve this object class java.awt.event ActionListener interface package. This class may be used java.swing Timer package to perform this method at a given time.

Comparator interface

Do not go into detail, more easily achieved with lambda expressions.

Guess you like

Origin www.cnblogs.com/zuotongbin/p/11729253.html