JAVA basics-interface concepts, categories, and differences with 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 interface, the structure in the Java language, with specific syntax and structure;

2. The 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 the interface used?

Java is 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.

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 interfaces that make Java single inheritance have the possibility of new extensions (to implement multi-inheritance );

Iii. Type level structure

Java interfaces (and abstract classes) are generally used as the starting point of a class 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 interface (including method definition)

Public interface ActionListener

{

Public abstract void actionreceivmed (ActionEvent event );

}

2. ID interface (no method or attribute definition is available)

An ID interface is an interface that does not have any methods and attributes. It does not have any semantic requirements for its classes. It only indicates that the class to implement it belongs to a specific type.

Public interface Serializable {};

3. Constant Interface

It refers to the use of Java interfaces to declare some constants, and then the classes implementing this interface use these constants.

Public interface AppConstants

{

Public static final DATA_SOURCE_NAME = "test ";

Public static final USER_NAME = "test ";

Public static final PASSWORD = "test ";

}

V. interface features

1. By default, all member variables in the Java interface are public, static, and final (can be omitted). They must be displayed for initialization, that is, the member variables in the interface are constants (uppercase, words are separated)

2. The methods in the Java interface are public by default, abstract type (can be omitted), no method body, and cannot be instantiated

3. Java interfaces can only contain public, static, final member variables and public, abstract member Methods

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

5. One interface cannot implement another interface, but it can inherit multiple other interfaces.

6. Java interfaces must use classes to implement their abstract methods.

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

8. You are not allowed to create an interface instance (instantiation), but you are allowed to define reference variables of the interface type. This reference variable references instances of classes that implement this interface.

9. A class can only inherit one direct parent class, but multiple interfaces can be implemented, indirectly implementing multi-inheritance.

 

Vi. Differences between Java interfaces and Java Abstract classes

The focus of object-oriented design is 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, this improves the transmission coupling between the two systems.

2. They cannot be instantiated.

3. All include abstract methods. These abstract methods are used to describe the services that the system can provide, but do not contain the method bodies.

Differences:

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 the Java Abstract class, 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 of this method to each class that implements this interface;

2. The implementation of the abstract class can only be given by the subclass, that is, the 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 extension points in the system. abstract classes are used to complete partial implementation. Some functions are also implemented through its subclass.

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.