Abstract Classes



Abstract classes are like normal classes for use. But they can not be sampled as classes. That's why you call abstract. Abstract classes can be thought of as a combination of an interface and a class. Object inheritance is very successful in common object use and prototyping. They are often used to create a common object to class clusters doing similar work and to ensure that these classes conform to a particular structure. When abstract class definition is done , abstract is used before class statement .

For a better understanding of abstract classes, review the following comparison chart;

Features Classes interfaces Abstract Classes
Sample It can be created. It can not be created. It can not be created.
methods They'il be body. It'il be without the body. According to the situation, the body may be without the body.
Heredity It can be transmitted by inheritance. It can not be transferred. Transferable.
Content Similarity Subclasses do not force you to move by a certain type. They force. They only enforce the methods defined by the abstract expression.

 

 

# Syntax


The syntax of abstract classes is analogous to the syntax of classes and interfaces, but with   an abstract expression per class statement .

<?php abstract class ExampleClassAbstract
{
    # Abstract yöntemler public görünürlüğe sahip ve gövdesiz olmalıdır.
    # Arayüz kullanımına benzer.
    # Alt sınıf aynı yöntemi içermesi için zorlar.
    abstract public function exampleFunction();

    # Ortak yöntemler gövdeli olur.
    # Nesne kalıtımına benzer.
    # Alt sınıflar için ortak yöntem oluşturur.
    public function commonFunction()
    {
        return 'Common Function';
    }
}

 

 

# Create Abstract Class


We have created Vechile :: class as an abstract class.

File: Vehicle.php
<?php abstract class Vehicle
{
    protected $color = 'White';
 
    abstract public function wayType();

    public function setColor(String $color)
    {
        $this->color = $color;
    }

    public function getColor() : String
    {
        return $this->color;
    }
}

We have forced the above abstract class to have the wayType () method of inheriting subclasses . Note that a method that has an abstract expression must have public visibility and no body.