Concepts, classes, and differences between JAVA interfaces and abstract classes

Source: Internet
Author: User

 

A Java Interface is a series of method declarations and a collection of Methods features. An Interface has only the features of a method and has no methods to implement it, therefore, these methods can be implemented by different classes in different places, and these implementations can have different behaviors (functions ).

I. Interface meaning:

1. Java interfaces, structures existing in the Java language, with specific syntaxes and structures; 2. A feature set of a class's methods is a logical abstraction.

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

Java interfaces do not have any implementations, Because Java interfaces do not involve representations and only describe public behaviors. Therefore, Java interfaces are more abstract than Java Abstract classes.

Java interface methods can only be abstract and public. Java interfaces cannot have constructors. Java interfaces can have public, static, and final attributes.

II. why is Java A Single-inherited language? To add new features to a specific class of an existing parent class, the solution is to add a parent class to its parent class under the OCP principle, or add a parent class to its parent class until it moves to the top of the class hierarchy. In this way, the design of the insertion ability of a specific class is changed to the modification of all classes in the overall hierarchy.

In the preceding example, you do not need to maintain all the classes in the hierarchy.

Iii. Interface accessibility:

In a hierarchical structure, any class can implement an interface, which affects all subclasses of this class, but does not affect any superclasses of this class. This class will have to implement the methods specified by this interface, and its subclass can automatically inherit these methods from this class, of course, you can also replace all these methods, or some of them, at this time, these subclasses are pluggable (and can be loaded with this interface type, which is passed to implement all its subclasses ).

Interfaces provide the scalability of association and method calling. The larger the scale of the software system, the longer the lifecycle. The interface ensures the flexibility and scalability of the software system and the maintainability.

It is precisely with the interface that makes Java single inheritance have the possibility of new extensions (implement multiple inheritance in a changed direction); 3. the Type level structure Java interface (and abstract class) is generally used as the starting point of a type level structure.

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

Iv. Java interface Classification 1. common interfaces (including method definitions) public interface ActionListener {public abstract void actionreceivmed (ActionEvent event );}

2. ID interface (no method or attribute definition) ID interface is an interface without any method or attribute. the identification interface does not have any semantic Requirements for the class to implement it. It only indicates that the class to implement it belongs to a specific type.

Public interface Serializable {}; 3. Constant interfaces are constants declared using Java interfaces, which are then used by classes implementing this 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. By default, all member variables in the Java interface are public, static, and final (can be omitted), and must be displayed for initialization. That is, the member variables in the interface are constants (uppercase, words are separated by "_". 2. The methods in the Java interface are public by default, abstract (can be omitted), and there is no method body, it cannot be instantiated. 3. Java interfaces can only contain public, static, final member variables and public, abstract member methods. 4. Interfaces do not contain constructor methods, cannot be instantiated 5. One interface cannot be implemented (implements) Another interface, but it can inherit multiple other interfaces. 6. Java interfaces must implement their abstract methods through classes. 7. When a class implements a Java interface, it must implement all the abstract methods in the interface. Otherwise, this class must be declared as an abstract class 8. You cannot create an interface instance (instantiate), but you can define reference variables of the interface type, the referenced variable references the instance of the class implementing this interface 9. A class can only inherit one direct parent class, but can implement multiple interfaces, indirectly implementing multiple inheritance.

6. Differences between Java interfaces and Java Abstract classes object-oriented design focuses on abstraction. Abstract classes and interfaces are both on the upper layer of the inheritance tree.

Similarities:

1. represents the abstraction layer of the system. When a system uses a class on the inheritance tree, we should try to declare the referenced variable as the upper abstraction type of the inheritance tree, in this way, the transmission coupling between two systems can be improved. 2. Neither can be instantiated. 3. abstract methods are included. These abstract methods are used to describe the services that the system can provide, but do not include the differences between the methods:

1. The biggest difference is that Java Abstract classes can provide partial implementation of some methods, but Java interfaces cannot. This is probably the only advantage of Java Abstract classes, but this advantage is very useful.

You can add a new method to the abstract class. All subclasses will automatically obtain this method. However, a new method is added to the Java interface, all classes that implement this interface cannot be compiled successfully. You must manually add the implementation method to each class that implements this interface. 2. The implementation of the abstract class can only be given by the subclass, that is, this implementation can only be defined in the hierarchy of inheritance defined by the abstract class; therefore, the efficiency of the abstract class as a type definition tool is greatly reduced.

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

The above shows that the Java interface is an ideal tool for defining the hybrid type. The mixed class indicates that a class has not only a primary type but also other secondary behaviors.

3. Combined with the advantages of abstract classes and Java interfaces in and, a classic design mode is available:

The Declaration type is still undertaken by the Java interface, but a Java Abstract class is provided at the same time, and this interface is implemented. Other classes belonging to this abstract type can choose to implement this Java interface, you can also choose to inherit this abstract class, that is to say, in the hierarchy, the Java interface is at the top, and then followed by the abstract class, the biggest advantages of the two can play to the extreme. This mode is the "Default adaptation mode ".

This mode is used in Java APIs and all follow certain naming rules: Abstract + Interface Name.

VII. General principles for using interfaces and abstract classes:

1. Using Interfaces as the window for system interaction with the outside world stands in the perspective of external users (another system). Interfaces promise users what services the system can provide and stand in the perspective of the system itself, interfaces define the services that must be implemented by the system. interfaces are the high-level abstraction types in the system. through interface interaction, the send coupling system A between two systems can interact through system B. This means that when system A accesses system B, it declares the referenced variable as the interface type in system B, this reference variable references the implementation class instance of the interface in system B.

Public interface B {}

Public class C implements B {}

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

2. the Java interface itself must be very stable. Once the Java interface is formulated, it is not allowed to meet any requirements. Otherwise, it will affect the external users and the system itself. 3. Use abstract classes to customize the extension points in the system, abstract class to complete partial implementation, but also some functions through its subclass to achieve

Related Article

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.