Learn Simpli

Free Online Tutorial For Programmers, Contains a Solution For Question in Programming. Quizzes and Practice / Company / Test interview Questions.

What is Abstraction in OOP – PHP

What is Abstraction in OOP?
Well for a better understanding of how abstract class works and where it can be used then keep reading the article it may help you to understand it properly. Let us take a real-time application example.

What is Abstraction in OOPS?
Abstraction, in general, is selecting only the essential data from the relevant object. The reason behind the abstraction is to remove the unwanted data that is not necessary. This helps in reducing the programming complexity and effort of the application. This can be achieved in PHP by using abstract classes. Even in the other OOPS languages. Assume you have to create an email ID account and you are asked to collect all the information about your customer. There are chances that you will come up with the following information about the customer name, username, address, and location, etc. Now you need to select only the information required to create an email id like name and username, not address, etc.

What is the abstract class in PHP?
An abstract class stands for the class which contains at least one abstract method within it, which has only the signature not the implementation of the body. Abstract class starts with the keyword abstract. And it can also have concrete methods.

What is the abstract method in PHP?
The methods declared only with signature and its name is called abstract methods and the access modifier should be public or protected and it can not be final because every class which is going to inherit the abstract class must implement all the abstract methods which are declared in the abstract class.

Behaviour or rules of an abstract class
1. An abstract class may not be instantiated because an abstract class contains abstract methods without a body so we cannot create an instance or object.

abstract class abstractClassName
{
    abstract protected function sayHello();

    public function greetEmployee() 
    {
        echo "Good morning";
    }
}

$abstractClasssObject=new abstractClassName();
$abstractClasssObject->printgreetEmployeeOut();

//Fatal error: Cannot instantiate abstract class abstractClassName

 

2. If a class that having any abstract method then the class must be declared as an abstract class

class regularClassName
{
    abstract protected function sayHello();
    public function greetEmplayee() 
    {
        echo "Good morning";
    }
}

$regularClassNameObject=new regularClassName();
$regularClassNameObject->greetEmplayee();
//Fatal error: Class regularClassName contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (regularClassName::sayHello)

 

3. An abstract method can not have an implementation.

abstract class abstractClassName
{
    abstract protected function sayHello()
    {
        echo "Hello";
    }
    public function greetEmployee() 
    {
        echo "Hello good morning";
    }
}

class inheritedClassName extends abstractClassName
{
    protected function sayHello() 
    {
        return "inheritedClassName";
    }
}

$class1 = new inheritedClassName;
$class1->greetEmployee();
//Fatal error: Abstract function abstractClassName::sayHello() cannot contain body

 

4. At the time of inheritance from an abstract class, all methods declared as abstract in the parent class must be implemented by the child

abstract class AbstractClass
{
    abstract protected function sayHello();
}

class inheritedClassName extends AbstractClass
{
    public function greetEmplayee() 
    {
        echo "Hi";
    }
}

$class1 = new inheritedClassName;
$class1->greetEmplayee();
//Fatal error: Class inheritedClassName contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (AbstractClass::sayHello)

5. While inheritance the access modifier of the methods in the child class must be the same access modifier or lesser.

abstract class abstractClassName
{
    abstract public function sayHello();
}

class inheritedClassName extends abstractClassName
{
    protected function sayHello() 
    {
        echo  "inheritedClassName";
    }
}

$class1 = new inheritedClassName;
$class1->greetEmployee();
//Fatal error: Access level to inheritedClassName::sayHello() must be public (as in class abstractClassName)

Summary

  1. An abstract class can have both abstract and non-abstract methods.
  2. It does not support multiple inheritances.
  3. An abstract class can have protected and abstract public methods.
  4. Abstraction helps to reduce the complexity of the design and implementation process of software.

Also, read Inheritance

2 thoughts on “What is Abstraction in OOP – PHP

Comments are closed.