Explain abstract class in PHP.

PHPObject Oriented ProgrammingProgramming

PHP5 comes with the object-oriented model with it, Some of the concepts of object-oriented model are: class, object, encapsulation, polymorphism, abstract and final classes, and methods, interfaces and inheritance, etc... In this article, 

we will discuss Abstract Class and it's features related to the object-oriented techniques in PHP. Also, we will learn the implementation of Abstract Class along with few examples.

But, before diving too deep,let's learn how to define abstract class.

We can declare a class as abstract by affixing the name of the class with the abstract keyword. The definition is very clear, the class that contains abstract methods is known as abstract class. Abstract methods define in the abstract class just have name and arguments, and no other code.

An object of an abstract class can't be made. Rather, we need to extend child classes that compute the definition of the function into the bodies of the abstract methods in the child classes and utilize these child classes to create objects.

Let's discuss some important facts about abstract classes in PHP:

1.An abstract class can have abstract along with non abstract methods.

Example:

<?php
   abstract class base {
      abstract function printdata();
      public function getdata() {
         echo "Tutorials Point";
      }
   }
   class child extends base{
      public function printdata(){
         echo "Good morning";
      }
   }
   $obj = new child();
   $obj->getdata();
?>

Output:

Tutorials Point

2.Cannot be directly instantiate.

Example:

<?php
   abstract class AbstractClass{
      abstract protected function calculate();
      public function adddata() {
         echo "Addition done";
      }
   }
   $obj=new AbstractClass();
   $obj->adddata();
?>

Output:

Fatal error: Uncaught Error: Cannot instantiate abstract class AbstractClass

3. Same (or a less restricted) visibility:

All child class must define all the methods marked as abstract in the parent class, with all these methods need to be defined with the same signature or less restricted signature. Suppose In parent class if we define an abstract method with protected visibilty, in the child class execution it should be defined with protected aorpublic, but not with private.

Example:

<?php
   abstract class AbstractBaseClass1{
      abstract public function addValue();
      abstract protected function getValue();
   }
   class ConcreteClass extends AbstractBaseClass1{
      protected function addValue() {
         return "ConcreteClass";
      }
      public function getValue() {
         return " Child Class";
      }
   }
   $classobj = new ConcreteClass;
   $classobj->addValue();
?>

Output:

Fatal errorAccess level to ConcreteClass::addValue() must be public (as in class AbstractBaseClass1)

4.An abstract method can not contain body:

Methods declared as abstract simply declare the method's signature - they cannot define anybody inside them. Although the body can be present inside a non-abstract method.

Example:

<?php
   abstract class ParentClass{
      abstract protected function printValue(){
         return "Good morning";
      }
   }
   class ClassA extends ParentClass{
      protected function printValue() {
         return "ConcreteClass1";
      }
   }
   $classobj = new ClassA;
   $classobj->printValue();
?>

Output:

PHP Fatal error: Abstract function ParentClass::printValue() cannot contain body

5.Any class that contains at least one abstract method must be declared as abstract class:

An abstract class can have abstract and non-abstract methods, but it must contain at least one abstract method. If it contains an abstract method then it should be declared as abstract.

Example:

<?php
   class AbstractClass {
      abstract protected function getValue();
      public function printData() {
         echo " Welcome to Tutorials Point";
      }
   }
   $obj=new AbstractClass();
   $obj->printData();
?>

Output:

PHP Fatal error: Class AbstractClass contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (AbstractClass::getValue)

6.Abstract class doesn't support multiple inheritance:

It doesn't support multiple inheritance.

Exmaple:

<?php
   Abstract class SuperClass{
      public abstract function test();
      protected function welcome(){
         echo "Good morning";
      }
   }
   class ClassA extends SuperClass{
      public function test(){
         echo "Hello";
      }
      protected function welcome(){
         echo "Good afternoon";
      }
   }
   class ClassB extends SuperClass{
      public function test(){
         echo "Hello";
      }
      protected function welcome(){
         echo "Good evening";
      }
   }
   class ClassC extends ClassA, ClassB{
      public function test1(){
         $c = new self();
         $c->welcome();
      }
   }
?>

Output:

Error

Explanation:

Here we have declare SuperClass as an abstract class having a method test() and welcome() and, ClassA and ClassB and concrete classes extend from an abstract class. Then we have tried to create ClassC extending from ClassA and ClassB. As it is evident from the code, on calling the method welcome() using object ClassC, it’s impossible for the compiler to choose whether it has to call ClassA’s welcome() or ClassB’s welcome() method. So, to stay away from such complications, PHP does not support multiple inheritance.

Note:

An abstract class can extend another abstract class, Abstract class can provide the implementation of the interface.

raja
Published on 07-Jun-2019 17:10:05
Advertisements