Searching...
Tuesday 16 October 2012

PHP : Class Abstraction

10:28 pm

Class Abstraction

Class Abstraction :

1.      A class for which we cannot create an instance or object is called as abstract class because it is an incomplete class.
2.      A class can have any no.of member classes and methods.
3.      A member functions present inside a class without having body(only method declaration) is known as abstract method.
4.      In a class if their exists atleast one abstract method then that class becomes incomplete ,so that it becomes an abstract class.
“abstract” is the keyword used for declaring an abstract method or abstrct class.
      We need to create a sub-class for the abstract class inside a sub-class we should define all the abstract method of the parent class otherwise that class becomes an abstract class and using that sub-class object we able to invoice member of abstract class.
<?php
abstract class Absclass
{
    public $a=10,$b=20;
    function addNum($x,$y)
    {
        return($x+$y);
    }
    abstract function subNum($x,$y);
   
}
?>
<?php
class Absimp1 extends Absclass
{
    function subNum($x, $y) {
        return($x-$y);
    }
}
?>
<h1>
    <?php
    $obj = new Absimp1();
    echo "sum=".$obj->addNum(11, 22);
    ?>
</h1>
OuPut:
sum=33 

0 comments:

Post a Comment