PHP Object-oriented (OOP): abstract methods and abstract classes

Source: Internet
Author: User

In the OOP language, a class can have one or more subclasses, and each class has at least one public method that accesses its interface as an external code. and abstract methods are introduced for the convenience of inheritance, let us first look at the definition of abstract and abstract methods to illustrate its purpose.

What is an abstract method? The method we define in our class is an abstract method , and the so-called no method body refers to the absence of curly braces and the contents of the method declaration, but rather the end of the method name with a semicolon at the time of declaration, plus a keyword when declaring an abstract method. abstract"to modify;

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 that there is a semicolon behind the abstract method; What is an abstract class? as long as there is one method in a class that is abstract, the class is defined as an abstract class , and the abstract class is also decorated with the "abstract" keyword, and in abstract classes there can be no abstract methods and member properties, but as long as one method is an abstract method, This class must be declared as an abstract class, decorated with "abstract".

Abstract class demo{    var$test;     Abstract function fun1 ();     Abstract function fun2 ();     function Fun3 () {        ...     }}

In the example above, an abstract class "Demo" is defined using "abstract" to define a member attribute "$test", and two abstract methods "Fun1" and "fun2", and a non-abstract method fun3 (); So how do we use abstract classes? The most important point is that the abstract class can not produce an instance object , so it can not be used directly, we mentioned many times before the class can not be used directly, we use the object through the class instantiation, then the abstract class can not produce an instance object we declare the abstract class what is the use of it? We use the abstract method as a template for subclass overloading, and defining an abstract class is equivalent to defining a specification that requires subclasses to obey, and after subclasses inherit abstract classes, the abstract methods inside the abstract class are implemented according to the needs of the subclasses. subclasses must implement all the abstract methods in the parent class, or else there are abstract methods in the subclass, then the subclass is abstract or the class cannot be instantiated ; Why do we have to inherit from the abstract class? Because sometimes we have to implement some functions 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;

<?Abstract classdemo{var $test; Abstract functionfun1 (); Abstract functionfun2 (); functionFun3 () {...    }}$demo=NewDemo ();//an abstract class cannot produce an instance object, so it is wrong to instantiate the object to the subclassclassTestextendsdemo{functionfun1 () {...    }    functionfun2 () {...    }}$test=NewTest ();//subclasses can instantiate an object because all abstract methods in the parent class are implemented?>

PHP Object-oriented (OOP): abstract methods and abstract classes

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.