Section 10 -- Abstract methods and abstract Classes -- Classes and Objects in PHP5 [10]

Source: Internet
Author: User
Tags php programming
/*
+ ------------------------------------------------------------------------------- +
| = Read this article for Haohappy <Core PHP Programming>
| = Notes in the Classes and Objects chapter
| = Translation-oriented + personal experiences
| = Do not repost it to avoid unnecessary troubles. Thank you.
| = Thank you for your criticism and hope to make progress together with all PHP fans!
+ ------------------------------------------------------------------------------- +
*/
Section 10-abstract methods and abstract classes
Object-oriented programs are built through the hierarchical structure of classes. in a single inheritance language such as PHP, class inheritance is tree-like. A root class has one or more sub-classes, and then inherits one or more sub-classes from each sub-class. of course, there may be multiple root classes to implement different functions. in a well-designed system, each root class should have a useful interface that can be used by application code. if our application code is designed to work with the root class, it can also work with any subclass inherited from the root class.
An abstract method is like a placeholder for a method in a subclass (which occupies a place but does not work). It is different from a general method-there is no code. if one or more abstract methods exist in the class, the class becomes an abstract class. you cannot instantiate an abstract class. you must inherit them and instantiate them. you can also regard the abstract class as a template of a subclass.
If you override all the abstract methods, the subclass will become a common class. if no method is overwritten, the subclass is still abstract. if a class contains an abstract method (even if there is only one), you must declare that the class is abstract and add abstract before the class keyword.
The syntax for declaring an abstract method is different from that for declaring a general method. The abstract method does not include the subject part in braces {} as the general method, and ends with a semicolon.
In Example 6.13, we define a Shape class containing the getArea method. however, since we do not know the shape, it is impossible to determine the area of the image, we have declared the getArea method as an abstract method. you cannot instantiate a Shape object, but you can inherit it or use it in an expression, as in Example 6.13.
If you create a class with only abstract methods, you define an interface ). to illustrate this situation, PHP has the keyword "interface" and "implements. you can use interfaces instead of abstract classes, and implements instead of extends to describe your class or use an interface. for example, you can write a myClass implements myIterface. these two methods can be selected based on personal preferences.
/* Note:
Two methods refer:
1. abstract class aaa {} (note that aaa only contains abstract methods, but does not have general methods)
Class bbb extends aaa {} (override the abstract method in aaa in bbb)

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.