PHP abstract class and interface similarities and differences "turn"

Source: Internet
Author: User
Tags what interface

1. The same point:

(1) Both are abstract classes and cannot be instantiated.

(2) The subclass of interface implementation class and abstract class must implement an abstract method that has already been declared.

2. Different points:

(1) interface need to implement, to use implements, and abstract class need to inherit, to use extends.

(2) A class may implement multiple interface, but a class can inherit only one abstract class.

(3) interface emphasizes the realization of a particular function, while the abstract class emphasizes its affiliation.

(4) Although the subclass of the interface implementation class and the abstract class must implement the corresponding abstract method, the implementation is different in form. Each method in interface is an abstract method, all just declared (declaration, no method body), the implementation class must be implemented. The subclass of abstract class can be implemented selectively. This option has a two point meaning: a) Not all methods in abstract class are abstract, only those whose crowns have abstractions are abstract and must be implemented by subclasses. Those that have no abstract method, must define the method body in the abstract class, B) The subclass of abstract class inherits it, the non-abstract method can be either directly inherited or overwritten, and the abstract method can be chosen to implement, but also left to its subclasses to implement, However, this class must also be declared as an abstract class. Abstract classes, and of course, cannot be instantiated.

(5) Abstract class is an intermediary between interface and class. Abstract class plays a connecting role in interface and class. On the one hand, abstract class is abstract and can be used to declare abstractions to standardize the functions that subclasses must implement, on the other hand, it can define the default method body for the subclasses to use or overwrite directly. In addition, it can define its own instance variables for use by subclasses through inheritance.

(6) Before the abstract method in the interface can not add the abstract keyword, the default implicit is an abstract method, and can not add the final keyword to prevent the inheritance of abstract methods. Abstract methods in abstract classes must be preceded by an abstract representation to display the declaration as an abstraction.

(7) The abstract method in the interface is public by default, and can only be public, and cannot be decorated with the private, protected modifier. Abstract methods in abstract classes can be modified with public, protected, but not private.

3. Interface applications

(1) Classes and classes require a specific interface to reconcile, regardless of how they are implemented.

(2) as an identity that is capable of achieving a specific function, it can also be a purely identifier of what interface method does not exist.

(3) A set of classes needs to be treated as a single class, and the caller is only connected to this set of classes through an interface.

(4) A specific number of functions need to be implemented, and there may be no connection between these features at all.

4. Application of abstract class

In a word, it can be used in situations where both a uniform interface is required and an instance variable or a default method is required. The most common are:

(1) Defines a set of interfaces, but does not want to force each implementation class to implement all interfaces. You can use the abstract class to define a set of method bodies, even an empty method body, and then select the methods that you are interested in to overwrite by subclasses.

(2) On some occasions, purely interfaces do not satisfy the coordination between classes and classes, and the variables that represent states in a class must differentiate between different relationships. Abstract mediation can be a good way to meet this point.

(3) standardize a set of mutually coordinated methods, some of which are common, state-independent, shareable, without subclasses, and others require individual subclasses to implement specific functions according to their specific state.

PHP abstract class and interface similarities and differences "turn"

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.