abstract classes in php

Upload: shijinbgopal

Post on 14-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Abstract Classes in PHP

    1/5

    Abstract Classes

    Abstract classes and methods are introduced in PHP 5An abstract class is a class which will usually be used as a base class for other classes to inherit f

    An abstract class is a class with or without data members that provides some functionality and lea

    remaining functionality for its child class to implement.

    The child class must provide the functionality not provided by the abstract class or else the child c

    becomes abstract.

    If a class contains abstract method then the class must be declared as abstract.

    Any method which is declared as abstract must not have the implementation part but the declarati

    The child classes which inherits the property of abstract base class, must define all the methods d

    abstract.

    Objects of an abstract and interface class cannot be created i.e. only objects of concrete class can

  • 7/29/2019 Abstract Classes in PHP

    2/5

    To define a class as Abstract, the keyword abstract is to be used

    abstract class Class_Name

    {

    // insert attribute definitions here

    // insert method definitions here

    }

    The child classes which inherits the property of abstract base class, must define all the methods declared as abstr

    An abstract method has no body

    Example of Abstract Class

    abstract class Furniture

    {

    private $height, width, length;

    public function setData($h, $w, $l)

    {

    $this->height = $h;

    $this->width = $w;

    $this->length = $l;

    }

    //this function is declared as abstract and hence the function

    //body will have to be provided in the child class

    public abstract function getPrice();

    }

    class BookShelf extends Furniture

    {

    private $price;

    public setData($h, $w, $l, $p)

    {

    parent::setData($h, $w, $l);

    $this->price = $p;}

    //this is the function body of the parent

    public function getPrice()

    {

    return $this->price;

    }

    }

    In the example, the method getPrice() in class Furniture has been declared as Abstract. Th

    responsibility of the child class to provide the functionality of getPrice(). The BookShelf

    Furniture class and hence provides the function body for getPrice().

  • 7/29/2019 Abstract Classes in PHP

    3/5

    Private methods cannot be abstract :

    An abstract class need not contain any abstract methods

    On the other hand, if a class is not declared as abstract, it cannot contain any abstract methods

    You can create a new abstract class that extends an earlier one

    abstract class MyAbstractBaseClass

    {

    public abstract function doSomething();

    }

    abstract class MyAbstractSubClass extends MyAbstractBaseClass {}

    Abstract classes ensure conformity because any classes derived from them must implement all ab

    within the class. Attempting to forgo implementation of any abstract method defined in the class r

    If a method is defined as abstract the

    declared as private (it can only be publ

    This is because a private method cann

  • 7/29/2019 Abstract Classes in PHP

    4/5

    Characteristics of Abstract Classes

    1. Single inheritance. Child classes can extend only one class at a time.

    2. Abstract classes cannot be instantiated

    3. Abstract classes can define class variables of type const only.

    4. Abstract class A can be extended by another abstract class B. Abstract class B can implement

    abstract functions in A.

    5. In the previous case, a child class C which extends abstract class B must implement all abstra

    well as the abstract functions in A which have not already been implemented in B.

    6. The signature of the concrete functions and abstract functions must be the same.

    7. The visibility of functions in the child classes must be the same or less restrictive than the pa

    protected abstract function can be implemented as either protected or public but not private.

    8. Declaring functions as static abstract throws a strict warning in PHP 5.2 or earlier, however, a

    allowed.

  • 7/29/2019 Abstract Classes in PHP

    5/5

    Characteristics of Abstract Classes

    1. Single inheritance. Child classes can extend only one class at a time.

    2. Abstract classes cannot be instantiated

    3. Abstract classes can define class variables of type const only.

    4. Abstract class A can be extended by another abstract class B. Abstract class B can implement

    abstract functions in A.

    5. In the previous case, a child class C which extends abstract class B must implement all abstra

    well as the abstract functions in A which have not already been implemented in B.

    6. The signature of the concrete functions and abstract functions must be the same.

    7. The visibility of functions in the child classes must be the same or less restrictive than the pa

    protected abstract function can be implemented as either protected or public but not private.

    8. Declaring functions as static abstract throws a strict warning in PHP 5.2 or earlier, however, a

    allowed.