The concept, classification and difference of Java interface with abstract class _java

Source: Internet
Author: User
Tags abstract inheritance mixed naming convention advantage

The Java Interface (Interface), a set of methods, is a collection of method features, an interface that has no method implementation of the characteristics of a method, so that these methods can be implemented in different places by different classes, and these implementations can have different behaviors (functions).

First, the meaning of the interface:

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

2. A collection of characteristics of a method of a class is a logical abstraction.

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

The Java interface itself has no implementation, because the Java interface does not involve representations and only describes public behavior, so Java interfaces are more abstract than Java abstract classes.

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

Second, why the use of interface Java is a single inherited language, to add new functionality to a specific class that already has a parent class, in the OCP principle, the solution is to add the parent class to its parent class, or to add the parent class to its parent class until it is moved to the top of the class hierarchy. Thus, the design of the pluggable nature of a specific class becomes a modification of all classes in the entire hierarchy.

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

Third, the interface with Hu may insertion:

Any class in a hierarchy can implement an interface that affects all subclasses of the class, but does not affect any superclass of the class. This class will have to implement the methods specified by this interface, and its subclasses can automatically inherit these methods from this class, of course, you can also choose to replace all of these methods, or some of them, at which point the subclass has the pluggable (and can be loaded with this interface type, passing all of his subclasses).

Interface provides the association and the pluggable nature of the method invocation, the larger the software system, the longer the lifecycle, the interface makes the software system flexible and scalable, and the pluggable aspect is ensured.

It is with the interface that the Java single inheritance has a new extension of the possibility (to achieve multiple inheritance); Type hierarchy Java interfaces (and abstract classes) are generally used as a starting point for a hierarchical structure of a type.

If a class already has a major super type, then by implementing an interface, the class can have another minor superclass, which is called a mixed type.

Four, Java interface classification

1. Common interface (including method definition) public interface actionlistener{public abstract void actionperformed (ActionEvent event);

2, Identity interface (no method and attribute definition) identity interface is an interface without any methods and properties. The identity interface does not have any semantic requirements for the class that implements it, it simply indicates that the class that implements it belongs to a specific type.

The public interface serializable{};3, a constant interface, refers to declaring constants with a Java interface and then using those constants by the classes that implement the interface.

Public interface Appconstants{public static final data_source_name= "test";p ublic static final user_name= "test";p ublic Static final password= "Test";}

V. Characteristics of the interface

1, the member variables in the Java interface are public,static,final type (all can be omitted), must be displayed initialization, that is, the member variable in the interface is a constant (uppercase, words separated by "_")

2, the Java interface method default is Public,abstract type (can be omitted), no method body, can not be instantiated

3, the Java interface can only contain public,static,final types of member variables and public,abstract types of member methods

4, there is no construction method in the interface, can not be instantiated

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

6. Java interface must implement its abstract method through class

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. Creating an instance of an interface (instantiated) is not allowed, but allows you to define a reference variable of an interface type that references an instance of the class that implements the interface

9, a class can only inherit a direct parent class, but can implement multiple interfaces, indirect implementation of multiple inheritance.

Six. Java interface and Java abstract class differentiation the focus of object-oriented design is abstraction. Both abstract classes and interfaces are located on the upper level of the inheritance tree.

Same point:

1, representing the abstraction layer of the system, when a system uses a class on the tree, as far as possible, reference variables should be declared as upper-level abstract types of the inheritance tree, which can improve the delivery coupling between the two systems 2, cannot be instantiated 3, all contain abstract methods, which are used to describe what services the system can provide, But does not contain the different points of the method body:

1, the biggest difference is that Java abstract classes can provide some of the partial implementation of methods, and Java interface is not; this is probably the only advantage Java abstract class, but this advantage is very useful.

You can add a new concrete method to the abstract class, and all subclasses get this method automatically, but a new method is added to the Java interface, all classes that implement this interface cannot be successfully compiled, and a class that implements the interface must be manually added to the implementation of the method;

2. The implementation of an abstract class can only be given by subclasses, which means that the implementation can only be in an inherited hierarchical structure defined by an abstract class, so an abstract class is compromised as a type-definition tool.

Java interface, any class that implements a method defined by a Java interface can have the type of this interface, and a class can implement as many Java interfaces as possible, so that the class has many types.

As you can see, the Java interface is the ideal tool for defining a mixed type, and a mixed class indicates that a class does not just have a main type of behavior, but it has other minor behaviors.

3, combined with the 1, 2 points in the abstract class and Java interface of their respective advantages, with the classic design pattern came out:

The work of declaring types is still assumed by the Java interface, but at the same time give a Java abstract class, and implementation of this interface, and other belong to this abstract type of concrete class can choose to implement this Java interface, you can also choose to inherit this abstract class, that is, in the hierarchy, the Java interface in the top, Then, following the abstract class, the two greatest advantages can be played to the extreme. This mode is the "default fit Mode".

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

Seven. General principles for using interfaces and abstract classes:

1, with the interface as the system and the outside of the window to interact with the external user (another system) of the perspective of the interface to the user to commit to the system to provide what services, stand in the system itself, the interface of the system must implement what services, An interface is the highest-level abstract type in a system. Interface interaction can improve the coupling system between two systems A through system B interaction, refers to system a access to system B, the reference variable is declared as the interface type in System B, the reference variable refers to the interface in system B of the implementation class instance.

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 developed, it is not allowed to encounter more, otherwise it will affect the outside user and the system itself 3, using abstract classes to customize the extension points in the system, abstract classes to complete the partial implementation, and some functions through its subclasses to achieve

The above detailed Java interface concept, classification and the difference with the abstract class is small to share all the content of everyone, hope to give you a reference, but also hope that we support cloud habitat community.

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.