Tuesday, February 7, 2012

Information about Class, Abstract class and Interface

Abstract class:

Similarities with Class:
Abstract class is a type of class which can have all the ability of a Class.  Abstract class can have data members member functions, All Access Specifier like Private, Public and Protected, Constructor & Destructor.

Controversies with Class:

Abstract class cannot be instantiate with its own.  Abstract class data members, member functions can be called through only its derived classes.

Every normal methods within the Abstract class should have its function definition or body content.

Every abstract methods within the Abstract class shouldn't have its function definition or body content.  If we use function prefix with abstract keyword then the function is called as abstract function.  Private visibility is
not possible to create abstract function.  Public and protected will works function.  If an abstract class
abstract function is written in one class, then the derived class should have the function definition in it; else
error will be displayed.

Advantages of Abstract Class:


If we have a common functionality used throughout the application then we can use a certain data members and its member functions.  This data member, member function are optional for the derived class.  The derived class can use the abstract class or not.. that is depends on the class nature.

Only one abstract class can be inherited into a normal class using the keyword 'extends'.

Interface:

Similarity with Class:

Partially we can call the interface as class.

Controversies with Class:

Interface cannot be instantiate with its own.  Interface does not have data members but have member functions without function definition; just list abstract functions.

Every methods in Interface should be Public or blank access speficiers (Visibility).

We can inherit multiple interfaces into a normal class using the keyword as 'implements'

Hirarchy of inheriting:

If we want to inherit both abstract class and interface class then we need to inherit the abstract class at first
and can inherit 'N' number of interfaces into the normal class.

Exceptions:

We can inherit only abstract also into a normal class.
We can inherit only interface (1 or more interfaces) also into a normal class.

Example:


abstract class hisabstract {
    private $hismember1 = 'didnt_called_yet';
    const mymember1 = 'test';
   
    function __construct(){
        echo "
calling hisabstract constructor..";
    }
   
    public function hispublic (){
      $this->hisprivate();
        echo "
Previously called method is: ".$this->hismember1;
      echo "
i am in myabstract class";
      $this->hismember1 = __method__;
    }
   
    protected function hisprotected (){
      echo "
Previously called method is: ".$this->hismember1;
      echo "
i am in myabstract class with function name as : ".__function__;
        $this->hismember1 = __method__;
    }
   
    private function hisprivate(){
      echo "
Previously called method is: ".$this->hismember1;
      echo "
i am in myabstract class with function name as : ".__function__;
        $this->hismember1 = __method__;
    }
   
    function __destruct(){
        echo "
calling hisabstract destructor..";
    }
   
    abstract function hisabstract_memfunc();
   
    final function hisabstract_finalfunc(){
        echo "
function from hisabstract class .. it is actully a final function.";
    }
}

interface myinterface {
    public function mypublic ();
}

interface yourinterface {
    function yourpublic ();
}

class mano extends hisabstract implements myinterface, yourinterface {
    function mypublic(){
        echo "
calling hisabstract function definition..".$this->hisprotected();
        echo "
myinterface function definition..".$this->mymember1;
        $this->yourpublic();
       
        return $this;
    }
   
    function yourpublic(){
        echo "
yourinterface function definition..".$this->hispublic();
    }
   
    function hisabstract_memfunc(){
        echo "
his abstract class abstract function definition..";
        $this->hisabstract_finalfunc();
    }
}

$x = new mano;
$x->mypublic()->hisabstract_memfunc();

No comments: