Detail the concept classification and distinction of the Java interface from the abstract class

  • 2020-05-16 06:59:31
  • OfStack

The Java interface (Interface), which is a declaration of a series of 1 methods, is a collection of method characteristics. An interface has only method characteristics and no method implementation, so these methods can be implemented by different classes in different places, and these implementations can have different behaviors (functions).

1. Interface meaning:

1.Java interface, structure existing in Java language, with specific grammar and structure;

2.1 the feature set of methods of the class is a kind of logical abstraction.

The former is called the "Java interface" and the latter is called the "interface".

The Java interface itself has no implementation, because the Java interface does not involve representations, but only describes the public behavior, so the Java interface is more abstract than the Java abstract class.

The methods of the Java interface can only be abstract and public, the Java interface cannot have constructors, and the Java interface can have public, static, and final properties.

2. Why interface Java is a single-inheritance language? If you want to add new functions to a specific class with an existing parent class, the solution under the principle of OCP is to add a parent to its parent class, or add a parent to the parent class of its parent class, until you move to the top of the class hierarchy. In this way, the design of pluggability for a concrete class becomes a modification of all the classes in the entire hierarchy.

When you have an interface, in the example above, you don't need to maintain all the classes in the entire hierarchy.

3. Interface with hu pluggability:

Any class in a hierarchy can implement an interface that affects all subclasses of this class but does not affect any superclass of this class. These will have to realize this method as described in the interface, and its subclasses can automatically from such inheritance, these methods, of course, also can choose from all these methods, or one 1 method, at that time, the subclass has a pluggable (and you can use this interface type load, transfer realized his all subclass).

The interface provides the pluggability of association and method calls. The larger the scale of the software system, the longer the life cycle. The interface guarantees the flexibility and extensibility of the software system, and the pluggability.

It is the interface that makes it possible to extend Java monoinheritance (variously implementing multiple inheritance). The Java interface (and abstract class)1 is generally used as the starting point for a hierarchical structure of a type.

If a class already has a primary supertype, then by implementing an interface, the class can have another secondary supertype, called a hybrid type.

4. Java interface classification

1. General interface (including method definition)public interface ActionListener{public abstract void actionPerformed(ActionEvent event); }

Identification interface (without any method or property definition) identification interface is an interface without any method or property. The identification interface does not have any semantic requirements for the class that implements it, but only indicates that the class that implements it belongs to a specific type.

public interface Serializable {}; 3. Constant interface means that the Java interface is used to declare some constants, which are then used by the class that implements the interface.

public interface AppConstants{public static final DATA_SOURCE_NAME="test";public static final USER_NAME="test";public static final PASSWORD="test";}

5. Interface features

1. The default member variables in Java interface are of type public,static and final, and must be initialized, that is, the member variables in the interface are constants (uppercase, separated by "_" between words).

2. The methods in Java interface are of type public and abstract by default. There is no method body and it cannot be instantiated

3. The Java interface can only contain member variables of type public,static,final and member methods of type public,abstract

4. There is no constructor in the interface and it cannot be instantiated

5. One interface cannot implement (implements) another interface, but it can inherit many other interfaces

6. The Java interface must implement its abstract methods through a class

7. When a class implements an Java interface, it must implement all the abstract methods in the interface, otherwise the class must be declared as an abstract class

8. You are not allowed to create an instance of the interface (instantiation), but you are allowed to define a reference variable of the interface type that references an instance of the class that implements the interface

9. A class can inherit only one direct parent class, but it can realize multiple interfaces and indirectly realize multiple inheritance.

6. The difference between the Java interface and the Java abstract class in object-oriented design focuses on abstraction. Abstract classes and interfaces are at the top of the inheritance tree.

Similarities:

Abstraction layer 1, on behalf of the system, when a system using 1 of the inheritance tree class, should try to put the reference variable declarations for the upper deck of the inheritance tree abstract types, which can improve between two systems to send coupling 2, 3 can be instantiated, contains abstract methods, these abstract method is used to describe the system can provide what services, but does not include the method body difference:

1. The biggest difference is that the Java abstract class can provide partial implementation of some methods, while the Java interface cannot. This is probably the only advantage of the Java abstract class 1, but it is very useful.

You can add a new concrete method to an abstract class, and all subclasses get the method automatically. However, if a new method is added to the Java interface, all classes that implement the interface will fail to compile successfully, and the implementation of the method must be manually added to each class that implements the interface.

2. The implementation of abstract class can only be given by subclass, that is, the implementation can only be in the hierarchical structure of inheritance defined by abstract class; So abstract classes are less effective as a type definition tool.

Java interface, any class that implements a method specified by an Java interface can have the type of this interface, and a class can implement any number of Java interfaces, so the class has multiple types.

As you can see above, the Java interface is an ideal tool for defining mixed types, which indicate that a class has not only the behavior of a primary type, but also other secondary behaviors.

3. Combining the respective advantages of abstract class and Java interface in points 1 and 2, a classic design pattern is produced:

Statement still borne by Java interface types of work, but at the same time give a Java abstract classes, and implements this interface, and the other with belong to the abstract type concrete class can choose to implement this Java interface, can also choose to inherit this abstract class, that is to say in the hierarchy, Java interface on top, and then followed by an abstract class, this is two of the biggest advantages can play to the extreme. This mode is known as the "default adaptation mode".

This pattern is used in the Java language, API, and it all follows a definite 1 naming convention: Abstract + interface name.

7. General principles for using interfaces and abstract classes:

1, with the interface as a system of interacting with the outside world window stood outside the user (the other one system) point of view, the interface to the user can provide what service commitment system, stand in the Angle of the system itself, must implement the interface for system which services, interface is the highest level of abstraction types in the system. Can improve interact through interfaces between two systems of a coupling system A B interact through the system, is refers to the system A B access system, the reference variable declarations for the system of B interface type, the reference ES15 variable reference system An instance of the implementation class for the interface in 6EN.

public interface B { }

public class C implements B { }

public class A { B a = new C(); }

Java interface itself must be very stable,Java interface 1, once designed, it is not allowed to more, otherwise it will affect the outside users and the system itself


Related articles: