The difference between an interface and an abstract class

Source: Internet
Author: User

What is the difference between an interface and an abstract class

What is the basis of your choice to use interfaces and abstract classes?


The concepts of interfaces and abstract classes are different. An interface is an abstraction of an action, and an abstract class is an abstraction of the root.

The abstract class represents what this object is. The interface represents what this object can do. For example, men, women, these two classes (if it's a class ...) ), their abstract class is human. Description, they are all human.

People can eat, dogs can eat, you can define "eat" as an interface, and then let these classes to implement it.

Therefore, in high-level languages, a class can inherit only one class (abstract class) (just as people cannot be both biological and non-living), but can implement multiple interfaces (eating interfaces, walking interfaces).

To summarize a few words:

1. Abstract classes and interfaces cannot be instantiated directly, and if instantiated, the abstract class variable must point to the subclass object that implements all the abstract methods, and the interface variable must point to the class object that implements all the interface methods.

2, abstract class to Quilt class inheritance, interface to be class implementation.

3, interface can only do method declaration, abstract class can do method declaration, can also do method to achieve

4, the variables defined in the interface can only be public static constants, the variables in the abstract class are ordinary variables.

5. Abstract methods in abstract classes must all be implemented by the quilt class, if the subclass can not fully implement the parent class abstract method, then the subclass can only be abstract class. Similarly, when an interface is implemented, if the interface method cannot be fully implemented, then the class can only be an abstract class.

6, abstract method can only declare, can not be implemented, interface is the result of design, abstract class is the result of refactoring

7, abstract class can have no abstract method

8, if there is an abstract method in a class, then this class can only be abstract class

9, abstract methods to be implemented, so can not be static, nor can it be private.

10, the interface can inherit the interface, and can inherit the interface more, but the class can only inherit one root.

1. Abstract classes and interfaces are used to abstract specific objects. However, the abstraction level of the interface is up to 2. Abstract classes can have specific methods and properties,  interfaces can only have abstract methods and immutable constants 3. Abstract classes are primarily used to abstract categories, and interfaces are primarily used to abstract functions.
4. In abstract classes, and without any implementations, derived classes must overwrite them. All methods in an interface must be non-implemented.

When you focus on the nature of a thing, use an abstract class, and when you focus on an operation, use an interface.


Abstract classes are much more powerful than interfaces, but the cost of defining abstract classes is high. Because of the high-level language (which is actually designed), each class can inherit only one class. In this class, you must inherit or write all of its subclasses.

All commonalities. Although the interface is much weaker in functionality, it is only a description of an action. And you can implement multiple interfaces in one class at the same time. In the design phase will reduce the difficulty.


Use of interfaces

Interface: interface

In PHP, we can specify what public external operations an object should have, which can be specified using interface.
The common method is the interface. Used to specify which public operation methods (interfaces) An object should be used for, which is also called an interface (a collection of public action methods)
namely: interface (interface structure, public method collection)

Public methods (interface methods)
Definition: A structure used to qualify an object that must have a common operating method, called an interface (interface)
Syntax: Defines the interface structure, using the interface keyword. The interfaces are defined in a number of public methods.


Attention:
1. Interface methods, access permissions must be public
2. There can be only public methods within the interface, no member variables exist
3. Interfaces can only contain methods that are not implemented, also called abstract methods, but do not use the abstract keyword.

The implements class implements the interface and completes with the keyword.


Thus, the class that implements the interface must implement all the abstract methods within the interface. And it is certain that this method must be a public external operation method.

Multi-Implementation: This function, in theory, can be achieved by abstract classes, but abstract classes, not professional.
The use of interfaces is professional, implementation, because PHP supports a multi-implementation, and only support single inheritance.

PHP Object interface Support, you can define class constants, interfaces can also inherit




Abstract methods and abstract classes

In the OOP language, a class can have one or more subclasses, and each class has at least one public method as
The external code accesses its interface. and abstract method is to facilitate the introduction of inheritance, we first look at the abstract class and
The definition of an abstract method illustrates its purpose.
What is an abstract method? The method that we define in the class without a method is an abstract method, the so-called no-side
The law refers to the absence of curly braces and the contents of the method declaration, but directly after the method name at the time of declaration.
With the end of the semicolon, plus a keyword "abstract" when declaring an abstract method;
For example:
Abstract function fun1 ();
Abstract function fun2 ();
The above example is the abstract method that "abstract" modifies without the method body "fun1 ()" and "fun2 ()", do not forget
There is also a semicolon behind the abstract method, so what is an abstract class? As long as there is one method within a class that is abstract
method, the class is defined as an abstract class, and the abstract class is also modified using the "abstract" keyword; in abstract class
Polygons can have methods and member properties that are not abstract, but as long as one method is an abstract method, the class must declare
For abstract classes, use "abstract" to decorate.


The above example defines an abstract class "Demo" that uses "abstract" to modify, in which the class defines a
A member property "$test", and two abstract methods "Fun1" and "fun2" have a non-abstract method fun3 ();
How do we use abstract classes? The most important point is that an abstract class cannot produce an instance object, so it cannot directly make
We have mentioned many times before that classes cannot be used directly, and we use objects that are instantiated by class
The image class cannot produce an instance object. What is the use of abstract classes? We are the modulo that the abstract method is to be overloaded as a subclass
Board, the definition of an abstract class is equivalent to defining a specification that requires subclasses to obey, subclass-Class relay abstraction
class, the abstract method inside the abstract class is implemented according to the needs of the subclass. Subclasses must have all the abstract methods in the parent class
are implemented, otherwise there is an abstract method in the subclass, then the subclass is an abstract class, or it cannot be instantiated. Why am I
Do we have to inherit from the abstract class? Because sometimes we have to implement some functions that must inherit from the abstract class, otherwise
These functions you can not realize, if inherit the abstract class, it is necessary to implement the abstract method of the class;



Single-Case mode

Singleton mode (duty mode): Simply put, an object (before learning the design pattern, need to know more about object-oriented thinking) is only responsible for a specific task; Singleton class: 1. Constructors need to be marked private (access control: Prevent external code from creating objects using the new operator), A singleton class cannot be instantiated in another class, only instantiated by itself; 2. A static member variable 3 that holds an instance of the class, and a public static method that accesses the instance (the Common getinstance () method instantiates a singleton class, You can detect if a class has been instantiated by using the instanceof operator in addition, you need to create a __clone () method to prevent an object from being copied (cloned) Why use PHP singleton mode? 1, the application of PHP is mainly in the database application, so there will be a large number of database operations in an application, using a singleton mode, you can avoid a large number of new operations to consume resources. 2, if the system needs to have a class to control some of the configuration information globally, then using the singleton mode can be easily implemented. This can be see the frontcontroller part of ZF. 3, in a page request, easy to debug, because all the code (such as database Operation class DB) is concentrated in a class, we can set the hooks in the class, output the log, so as to avoid var_dump everywhere, echo. Code implementation:
/1*** Pattern of design pattern * $_instance must be declared as a static private variable * Constructors and destructors must be declared private, preventing external programs from new* classes thereby losing the meaning of the singleton pattern * The getinstance () method must be set to public, this method must be called * To return a reference to the instance *:: operator can only access static variables and static functions * The new object consumes memory * Usage scenarios: The most common place is the database connection. * Once an object is generated using singleton mode, the object can be used by many other objects. */
Class Danli {//Save static member variable for class instance private static $_instance;//private tag construction method private Function __construct () {echo ' This is a Cons Tructed method; ';} The __clone method is created to prevent the object from being copied to the Clone public Function __clone () {Trigger_error (' clone was not allow! ', e_user_error);}//Singleton method, public static method that is used to access the instance publicly static function getinstance () {if (! Self::$_instance instanceof self) {self::$_instance = new Self;} return self::$_instance;} Public Function test () {echo ' call method succeeded ';}} The class that instantiates the private tag constructor with new will have an error//$danli = new Danli (); Correct method, use double colon:: operator to access static method to get instance $danli = Danli::getinstance (); $danli->test (); Copying (cloning) an object will result in a e_user_error$danli_clone = Clone $danli;

The difference between an interface and an abstract class

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.