1)
Abstract Class Example
2) Abstract Class Example(taking value from user)
3) Abstract Class Example (If same function name in both parent and child class)
Note:
<?php
abstract class
Fruit
{
public function Orange()
{
echo "This is
Orange<br>";
}
abstract public function Apple(); //abstract function declaration
}
class Fruit1
extends Fruit
{
public function Apple() // //abstract function definition
{
echo " This is
Apple";
}
}
$obj=new Fruit1();
$obj->Orange();
$obj->Apple();
?>
Output:
This is Orange
This is Apple
This is Apple
2) Abstract Class Example(taking value from user)
Demo.html
<html>
<body>
<form
method="POST" action="add_abs1.php">
Number 1 :
<input type="number"
name="num1"/><br><br>
Number 2 :
<input type="number" name="num2"/><br><br>
<input
type="submit" name="Add" value=" ADD"/>
</form>
</body>
</html>
add_abs1.php
<?php
$n1=$_POST['num1'];
$n2=$_POST['num2'];
abstract class Add
{
abstract public function
addition($n1,$n2);
}
class add1 extends
add
{
public function addition($n1,$n2)
{
//$c=$n1+$n2;
echo "Addition is :
$c" ;
}
}
$obj=new add1();
$obj->addition($n1,$n2);
?>
3) Abstract Class Example (If same function name in both parent and child class)
<?php
abstract class
Animal
{
public $name;
public $age;
public function Describe() //normal function
{
return
$this->name.".".$this->age." "."is years
Old"." " ;
}
abstract public function Greet(); //abstract function declaration
}
class Dog extends
Animal
{
public function Greet() // abstract function definition
{
return
"<br>Woof!";
}
public function Describe()
{
return parent
::Describe()."and I am a Dog!";
// Here we are calling
parent class Describe function using parent:: Describe() in child class.
}
}
$animal= new Dog();
$animal->name="Seru";
$animal->age=5;
echo
$animal->Describe();
echo
$animal->Greet();
?>
Output:
Seru.5 is years Old and I am a Dog!
Woof!
Woof!
Note:
When you have same
function name in child and parent class then use
parent ::function _name() in child class function to call parent class function
like parent ::Describe()
No comments:
Post a Comment