Abstract Class

What is Abstraction?

Abstraction is process of hiding the implementation details and showing only the functionality.it shows only important things to the user and hides the internal details.
Example:-
When you start a car then you don’t know what is the mechanism work in this, that is called Abstraction.

Abstract Class

A class defined with abstract keyword, that class called Abstract Class. Abstract classes are those classes which can not be directly initialized.
OR
you can not create object of abstract classes.
Note:-
(i) abstract classes can hold abstract method and other method.
(ii) At least one abstract method must be in abstract class.
(iii) Abstract classes always created for inheritance purpose.
abstract class classname{
}

Example:- Abstract class and abstract method

<?php
abstract class Area {
	abstract public function getArea();
	public function showArea()
	{
	 echo $this->getArea();
	}
}
class rectangle extends Area
{
	public $length=100;
	public $width=50;   
	public function getArea()
	{
	 return 'Rectangle Area is '.($this->length)*($this->width);
	}
}
class triangle extends Area
{
	public $base=50;
	public $height=20;   
	public function getArea()
	{
	 return 'Triangle Area is '.($this->base)*($this->height)/2;
	}
}

$rec_obj = new rectangle;
$rec_obj -> showArea();

$tri_obj = new triangle;
$tri_obj -> showArea();
?>
Output:- Rectangle Area is 5000
Triangle Area is 500

Can not create object of abstract classes

If we create a object of the abstract class then show fatal error.

<?php
abstract class student {
	public $name = "john";
	public function student_name() {
		return $this -> name;
	}

}

$stu_obj = new student;
echo $stu_obj -> student_name();
?>
Output:- Fatal error: Cannot instantiate abstract class student

Abstract method

You can create any method abstract using keyword abstract.
Syntax:-
abstract function functionname(){
}

Note:-
(i)create abstract method either in abstract class or interface.
(ii) Abstract function must be declare in Abstract class and define in child class.
Example:-

<?php 
abstract class country {
abstract public function country_name($cn);
}
class India extends country
{
public function country_name($cname)
{
echo 'Country name is '.$cname;
}	
}
$in_obj = new India;
$in_obj -> country_name('India');
?>
Output:- Country name is India

Why use Abstract class and method?

Many programmer thinks, what is purpose of use of abstract class and method.

Firstly without use abstract class program.

In the below example, when call Animals class and givemilk() method then show animals give us milk but all animals not give milk so it has no sense.

<?php 
class Animals { 
    public function givemilk() {
    	//No sense 
       echo 'animals give us milk.'; 
    } 
 } 

class Cow extends Animals { 
    public function givemilk() {
    	echo 'Cow give us milk.';
   } 
} 

class Buffallo extends Animals { 
    public function givemilk() {
    	echo 'Buffallo give us milk.';
   } 
} 

$cow_obj=new Cow;
$cow_obj->givemilk();  

$buf_obj=new Buffallo;
$buf_obj->givemilk();  

$ani_obj=new Animals;
$ani_obj->givemilk(); 
?>
Output:- Cow give us milk.
Buffallo give us milk.
animals give us milk.

With Abstract class program

In the below eg. we declare abstract class Animals and it has abstract method givemilk(), you know abstract method is declare in the abstract class and define in the child class. so if we create a object of the abstract class and call the abstract method then show fatal error.

<?php 
abstract class Animals { 
   abstract public function givemilk();
} 

class Cow extends Animals { 
    public function givemilk() {
    	echo 'Cow give us milk.';
   } 
} 

class Buffallo extends Animals { 
    public function givemilk() {
    	echo 'Buffallo give us milk.';
   } 
} 

$cow_obj=new Cow;
$cow_obj->givemilk();  

$buf_obj=new Buffallo;
$buf_obj->givemilk();   
?>
Output:- Cow give us milk.
Buffallo give us milk.