Thursday 8 February 2018

abstraction in PHP


Abstraction means providing important or necessary features without giving internal or background details.
It shows only useful information.

Real Life Example: 
When user is using Mobile application he doesn't know internal working of all functions like calling, send sms but he can call or send sms without knowing internal details. His main aim is using the mobile phone not to know about internal structure.

Abstract class:
Abstract class contains both concrete (complete) and abstract (incomplete) method.
At least one method should be abstract in abstract class. It may contain more than one abstract and concrete method.
We cannot create object of abstract class.
To declare a class or method as abstract, Use abstract keyword before class or method name.
Concrete method contains method definition but abstract method contains method declaration.
Abstract method ended with semicolon.
You know that abstract method is incomplete so we have to complete (define) the method in child class for that create child class (using extends keyword).When you define abstract method in child class then do not use abstract keyword before method name.

Abstract method Syntax:
abstract visibility function function_name ();
Example:
abstract public function show ();

Concrete method syntax:
visibility function function_name ()
{
  // statements
    }
Example:
public  function  display()
{
// statements
}

Example 1:
<?php
abstract class Fruit
{
public function Orange()       //concrete method or function
{
echo "This is Orange<br>";
}

  abstract public function Apple();  //abstract method declaration

}

class Fruit1 extends Fruit
{
          public function Apple()     //abstract method definition
{
echo " This is Apple";
}

}


$obj=new Fruit1();
$obj->Orange();
$obj->Apple();
?>
Output:
This is Orange
This is Apple

Example 2:
<?php
abstract class Sum
{
 abstract public function addition($a,$b);  //abstract method declaration
}

class Demo extends Sum
{
 public function addition($a,$b)     //abstract method definition
{
    $c=$a+$b;
    echo "sum is:$c";
}
}
 $obj=new Demo();
$obj->addition(23,24);
?>
Output:
sum is :47

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...