Senior Programmer essential knowledge: interfaces and abstract classes What is the difference?

In Java can be used to define the interfaces and abstract classes allows to achieve a plurality of types; however, interfaces and abstract class has two main differences:

  • The abstract class allows realization comprising certain methods, the interface is not allowed; [Java8 have been provided in the default method]
  • From the design perspective, in order to achieve the type defined by the abstract class, the class must be defined as a subclass of the abstract class, which limits its level in the class diagram, but the interface is not this level of restriction;

In the actual development, we should follow a principle - the interface is better than abstract classes, mainly due to the interface has advantages in three areas:

  • As long as the realization of the new interface, you can add new functionality to existing classes; but a class can only inherit from a parent class;
  • It is very suitable for defining a mixin (hybrid type). Comparable is a mixin interface that is because any type of object can implement this interface to provide the ability to compare objects, mixed with their main function.
  • The interface is not strictly required level of classes that implement it more flexible in design.

Knowledge combing

Java abstract classes

Design abstract class common between classes and interfaces, the target is an abstract class for a common design, it allows to achieve a plurality of types.

Contains abstract methods of the class is called abstract class; abstract class can not be instantiated, if a class inherits an abstract class, and you want to instantiate, all abstract methods abstract class will need to implement.

Application abstract class in Java, the most classic is the "skeleton model" - and with the use of interfaces, allowing designers also have the advantage of abstract classes and interfaces - the evolution of abstract classes is easier than the evolution of the interface, while allowing the system other classes do not use this skeleton implementation class.

In the "skeleton model", the role of the interface is still defined type, skeleton implementation class is an abstract class, responsible for all work related to the interface. Application of a large set of framework "backbone model", for example: AbstractCollection, AbstractList, AbstractMap and the AbstractSet, some common operations are implemented on the skeleton implementation class, such concrete ArrayList and LinkedList can focus on achieving their characteristics avoid writing duplicate code.

Senior Programmer essential knowledge: interfaces and abstract classes What is the difference?

"Skeleton model" sounds a bit like Template Method design pattern, but there are still between the two are not the same: target the "skeleton model" is to avoid duplicate code from the code level, the goal of "template method" is at the design level designed for a certain type of things abstract and extension points.

Java Interface

Semantics in Java interface is "is like" - any class that implements an interface, it looks like you have this ability to interface.

In Java, the interface can be used to do three things:

  • Type definition;
  • As a constant interface to save some constants;
  • Only as a label.

However, in Article 19 "Effective Java" pointed out that the interface should only be used to define the type.

Before Java8, declared in the interface method can only occur, implementation can not occur. Java 8's ability to do the interface enhancements:

  1. Interfaces may be the default method;
  2. Interfaces may be static method;
  3. It provides FunctionInterface this concept.

1.Java 8 introduced the main motivation for default method is: to be supported in Java 8 lambda expressions, which need to make changes to the original JDK many of the interface, but due to the characteristics of the interface (its implementation class must implement the interface in all methods declared) limits, if modified directly, it will lead to existing Java applications to upgrade to Java 8 compiler error when all - this is unacceptable.

In the actual development, for the default method of use you can pay attention to several points:

  • You can follow the need to expand the interface, without having to worry about the impact of existing implementation class;
  • Abstract classes and interfaces so that almost no difference, and can safely get behind the interface used, while the advantages can be obtained using the default method abstract class;
  • Save a lot of tools to provide an interface, and of these tools can be implemented on the interface, such as a method of this class can Collections are placed in the Collection interface;

2, with similar default method, static method can also be implemented in the interface, but you in the course can not cover the static method, therefore, if there are ways you do not want to be destroyed a specific category, it is the statement may be as static method .

In the actual development, using static method you can pay attention to several points:

  • static method belongs to an interface, not belonging to an object;
  • static method is suitable for implementing the method relating to the interface means, for example check null, set ordering and the like;
  • The method Collection Collections to the mobile interface, enabling developers to easily find the corresponding method, this method is also more suitable to use static method.

3, in order to support lambda expressions, Java 8 introduces a new comment:

@FunctionalInterface // method Notes

If an interface has been modified this comment, this interface will be called functional interface. Functional annotation interfaces is not required, but is a good practice.

If an interface matching "function interface" is defined, then did to the notes are not affected. Plus the notes to better allow the compiler to be checked. If writing is not functional interface, but added @FunctionInterface, then the compiler will complain. .

Guess you like

Origin www.cnblogs.com/CQqf2019/p/11124718.html