Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - Inheritance and Other Advanced OOP Fea...
MISCELLANEOUS

Inheritance and Other Advanced OOP Features
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2010-06-02

    Table of Contents:
  • Inheritance and Other Advanced OOP Features
  • Interfaces
  • Implementing a Single Interface
  • Abstract Classes

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Inheritance and Other Advanced OOP Features - Abstract Classes


    (Page 4 of 4 )

    An abstract class is a class that really isn’t supposed to ever be instantiated, but instead serves as a base class to be inherited by other classes. For example, consider a class titled Media, intended to embody the common characteristics of various types of published materials, such as newspapers, books, and CDs. Because the Media class doesn’t represent a real-life entity, but is instead a generalized representation of a range of similar entities, you’d never want to instantiate it directly. To ensure that this doesn’t happen, the class is deemed abstract. The various derived Media classes then inherit this abstract class, ensuring conformity among the child classes, because all methods defined in that abstract class must be implemented within the subclass.

    A class is declared abstract by prefacing the definition with the wordabstract, like so:

    abstract class classname
    {
           // insert attribute definitions here
           // insert method definitions here
    }

    Attempting to instantiate an abstract class results in the following error message:

    Fatal error: Cannot instantiate abstract class staff in
    /www/book/chapter06/class.inc.php.

    Abstract classes ensure conformity because any classes derived from them must implement all abstract methods derived within the class. Attempting to forego implementation of any abstract method defined in the class results in a fatal error.


    ABSTRACT CLASS OR INTERFACE?

    When should you use an interface instead of an abstract class, and vice versa? This can be quite confusing and is often a matter of considerable debate. However, there are a few factors that can help you formulate a decision in this regard:

    • If you intend to create a model that will be assumed by a number of closely related obects, use an abstract class. If you intend to create functionality that will subsequently be embraced by a number of unrelated objects, use an interface.
    • If your object must inherit behavior from a number of sources, use an interface. PHP classes can inherit multiple interfaces but cannot extend multiple abstract classes.
    • If you know that all classes will share a common behavior implementation, use an abstract class and implement the behavior there. You cannot implement behavior in an interface.

    Please check back next week for the conclusion to this series.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap