Home > PHP > OOPS in PHP 5 Tutorial – Abstract Class

OOPS in PHP 5 Tutorial – Abstract Class

This article will guide you on the Abstract Class used in PHP5. Also it will give an insight on what abstract class is all about when we talk about Object Oriented Programming Concept.

What is an Abstract Class?
An abstract class defines the basic skeleton for the class. It contains attributes and members but some members are incomplete and is waiting for some other class to extend it through inheritance so that the derived class provides a full functionality for the incomplete methods. A abstract class cannot be instantiated and it can only be extended. A class prefix with “abstract” keywords are abstract class. If a method is defined as abstract then it cannot be declared as private (it can only be public or protected).

Syntax:

< ?
abstract class classname
{
      //attributes and methods
     .
     .
     abstract function methodname
}
 
class derived extends classname
{
     function methodname
}
?>

Example 1 – Basic Abstract Class Usage

< ?
    abstract class employee
   {
       protected $empname;
       protected $empage;
 
       function setdata($empname,$empage)
      {
          $this->empname = $empname;
          $this->empage = $empage;
      }
 
      abstract function outputData();
   }
 
   class EmployeeData extends employee   //extending abstract class
   {
        function __construct($name,$age)
        {
           $this->setdata($name,$age);
        }
 
       function outputData()
       {
          echo $this->empname;
          echo $this->empage;
       }
   }
 
    $a = new EmployeeData("Hitesh","24");
    $a->outputData();
?>

Output:
Hitesh
24

Your email:

 


Explanation for the Example 1:

  • Here i have made employee class an abstract class
  • Employee class has 2 data members $empname and $empage. It also has a abstract method outputData() and public method setData()
  • Now i am extending the employee class in EmployeeData. It contains the functionality for the abstract method outputData defined in employee class


Example 2 – Abstract Class Error

< ?
abstract class employee
{
     protected $empname;
     protected $empage;
 
     function setdata($empname,$empage)
     {
          $this->empname = $empname;
          $this->empage = $empage;
     }
 
     abstract function outputData();
}
 
class EmployeeData extends employee
{
 
      function __construct($name,$age)
      {
            $this->setdata($name,$age);
      }
}
$a = new EmployeeData("Hitesh","24");
$a->outputData();
//Will generate error as the EmployeeData  class doesn't has the abstract method defined
?>

Output
This will give you an error “Class EmployeeData contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (employee::outputData)”

Example3 – Multiple Abstract Class

< ?
abstract class employee
{
    protected $empname;
    protected $empage;
 
    function setdata($empname,$empage)
    {
         $this->empname = $empname;
         $this->empage = $empage;
    }
 
    abstract function outputData();
}
 
abstract class EmployeeData extends employee
{
      abstract function setDataForEmployee();
}
 
class Payment extends EmployeeData
{
      function setDataForEmployee()
      {
             //Functionality
      }
 
      function outputData()
      {
         echo "Inside Payment Class";
      }
}
 
$a = new Payment();
$a->outputData();
?>

Output: “Inside Payment Class”


Custom Search

Popular Articles:

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • IndianPad
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • RSS
  • Technorati
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Reddit
  • Add to favorites
  • PDF
  • Twitter
Categories: PHP Tags: ,
  1. November 5th, 2007 at 04:43 | #1

    An abstract class defines the basic skeleton for the class. It contains attributes and members but some members are incomplete and is waiting for some other class to extend it so that the derived class provides a full functionality for the incomplete methods. A abstract class cannot be instantiated and it can only be extended. A class prefix with “abstract” keywords are abstract class. If a method is defined as abstract then it cannot be declared as private (it can only be public or protected).

  2. January 8th, 2008 at 06:44 | #2

    Hi,Really Helpful esp. your Articles about PHP5,i hope it will very useful for PHP Programmer who come from PHP 4 vesion into Version 5

  3. anil sharma
    February 12th, 2008 at 05:52 | #3

    Very well explained… Same can be enhanced by writing on “Why we need an abstract class? with vary practical example example”

  4. April 24th, 2009 at 01:53 | #4

    Hey its rally very very good example which makes abstract class concept much easier to understand.
    Thanks

    In example 2 even if you dont write following lines

    $a = new EmployeeData(“Hitesh”,”24″);
    $a->outputData();

    It will show you an error.

  5. October 22nd, 2009 at 22:53 | #5

    Hi Hitesh, great explanation.. keep it up.. !!

    Can you pls tell me ..

    I think ABSTRACT class is used for same function in different class used for different purposes like polymorphism.. is it true?

    Why we need an abstract class?

    • November 3rd, 2009 at 02:46 | #6

      Hi Manoilayans,
      We require abstract to define an contract that an class with implement. Also abstract can hold constants and non-abstract methods that an class can use to perform operation.

      Example:
      If we define an abstract class called 4-Wheeler that will have Engine, Tyres etc. Now we can take any 4-wheeler car that will have to implement the abstract methods defined in the abstract class. The abstract class is not concerned about the implementation.

      Hope this will solve your query.

      Thanks,
      Hitesh Agarwal

  6. debranjan bera
    December 28th, 2009 at 00:29 | #7

    what is basic different between abstract class & normal class with inheritance.
    ?

  7. April 23rd, 2010 at 08:30 | #8

    @debranjan bera
    This is what I’ve been particularly trying to grasp. The basic difference is that an abstract class cannot be instantiated while a normal class used as a base can. Indeed, there are plenty of situations where I’ve created a base class that was never meant to be used directly, only inherited from, so an abstract class makes sense in that regard.

    However, I still don’t understand why it’s considered good practice to NOT implement anything in the abstract class. To me, that defeats the whole purpose of a base class – to be able to define things once and once only for use throughout a program. Why declare something in the abstract class only to re-declare it in every subclass? Sure, it allows you to look at the abstract class for a quick visual overview of what all subclasses will contain, but it makes little sense to implement nothing there simply to follow convention.

    The code examples in this page make use of implementation in the abstract class, so I’m not seeing the value behind this “best practice” I’ve been reading about. Any thoughts?

    • April 23rd, 2010 at 09:30 | #9

      First of all public/protected variables you declare in abstract class is automatically inherited in base class and you don’t need to declare the variable at all location.

      Abstract also allows you to perform operation specific to an object, say for e.g. Car is an abstract class, car will have steering, wheels, window etc. So abstract class will implements all the common operation that car performs like how steering wheel works etc, but the implementation of how gears works(automatic/manual) can be inside base class that extends the abstract car class.

      Hope this answers your query.

  8. uday victory
    June 25th, 2010 at 04:01 | #10

    hi Hitesh,
    excellent explanation about abstract class,
    and one more thing, i hope u will be helpful,
    sir, i want complete explanation about abstract method,

    And Why we need an abstract method ?
    explain me with examples one or two please ,,,,,,,,,,,,
    Thank u,
    uday victory

  9. Nirvikar
    August 11th, 2010 at 01:12 | #11

    hi,
    can you tell me where i implement abstract class in our codding please tell me an example

    • August 12th, 2010 at 10:39 | #12

      Hi Nirvikar,
      There are various scenarios where you would require abstract class. Common example would Car Object, Car has wheels, steering wheel, door all these things are common to all the Cars so this can be abstract and can be inside abstract class but functionality like braking system, engine starting mechanism can vary from car to car, this can be written in abstract class as abstract methods and it’s implementation would be inside concrete class.

      Hope this helps in understanding the concept of abstract class.

      Thanks,
      Hitesh Agarwal

  1. No trackbacks yet.