Explain about Abstraction?
Posted by Raj
In this tutorial ,I will explain about abstract class in PHP, how to declare and use of an abstract class.
Abstract Class:
It may contain one or more abstract methods.abstract classes may not be instantiated.The child classes which inherits the property of abstract base class, must define all the methods declared as abstract.
Any class that contains at least one abstract method must also be abstract.
if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.
Abstract Class:
It may contain one or more abstract methods.abstract classes may not be instantiated.The child classes which inherits the property of abstract base class, must define all the methods declared as abstract.
Any class that contains at least one abstract method must also be abstract.
if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.
Example:
<?php
abstract class AbstractClass
{
abstract protected function getValue();
abstract protected function setValue($val);
public function Display() {
print $this->getValue() . "\n";
}
}
class ChildClass1 extends AbstractClass
{
protected function getValue() {
return "ChildClass1";
}
public function setValue($val) {
return "ChildClass{$val}";
}
}
class ChildClass2 extends AbstractClass
{
public function getValue() {
return "ChildClass2";
}
public function setValue($val) {
return "ChildClass{$val}";
}
}
$class1 = new ChildClass1;
$class1->Display();
echo "<br>";
echo $class1->setValue('1') ."\n";
echo "<br>";
$class2 = new ChildClass2;
$class2->Display();
echo "<br>";
echo $class2->setValue('2') ."\n";
?>
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
Abstract Class,
oop,
OOP interview questions and answers,
PHP,
PHP Abstract Class,
PHP interview questions and answers
.You can leave a response, or trackback from your own site.