Abstract Class and Interface
About abstract class

A class with data and without data member that provide some functionality and some functionality develop in child class so this type of class is called abstract class. For create abstract class we use abstract keyword.

Note :- we can not create object of the abstract class.

Syntact to implementing abstract class

abstract class base {
    variable here
 
           public abstract function test();
 
}
 
 
class derived extends base {
 
     public function test() {
      code here
   }
}

Example 1:-

<?php
abstract class abstractclass
{
  
    abstract protected function input();
    abstract protected function getvalue($x);
 
    public function show() {
        print $this->input() . "n";
    }
}

class demo1 extends abstractClass
{
    protected function input() {
        return "This is demo1";
    }

    public function getvalue($a) {
        return "Value : $a";
    }
}

class demo2 extends abstractClass
{
    public function input() {
        return "This is demo2";
    }

    public function getvalue($b) {
        return "Value : $b";
    }
}

$obj1 = new demo1();
$obj1->show();
echo $obj1->getvalue(5) ."n";

$obj2 = new demo2();
$obj2->show();
echo $obj2->getvalue(6) ."n";
?>

Output

This is demo1
Value : 5
This is demo2
Value : 6

Interface

For interface all the methods by default are abstract methods only. So one cannot declare variables or concrete methods in interfaces.

Example 2:-

<?php

interface mydemo
{
    public function show();
}

class demo implements mydemo{
public function show(){
    echo "This is show function";
}
}

$obj=new demo();
$obj->show();
?>

Interface as Mulitple Inheritance

<?php
interface mydemo1
{
    public function show1();
}

interface mydemo2
{
    public function show2();
}

interface mydemo3 extends mydemo1, mydemo2
{
    public function show3();
}

class demo implements mydemo3
{
    public function show()
    {
    }

    public function show2()
    {
    }

    public function show3()
    {
    }
}
?>

Difference between Abstract class and Interface

Abstract Class

Interface

For abstract class a method must be declared as abstract. Abstract methods doesn’t have any implementation. For interface all the methods by default are abstract methods only. So one cannot declare variables or concrete methods in interfaces.
The Abstract methods can declare with Access modifiers like public, internal, protected. All methods declared in an interface must be public.
Abstract class can contain variables and concrete methods. Interfaces cannot contain variables and concrete methods except constants.
A class can Inherit only one Abstract class A class can implement many interfaces

Previous
Next