QUIZGUM

Coding Class

Quizgum : abstract

abstract

Now let's look at the abstract keyword.
Earlier we learned about the interface.
I used to set rules.
The abstract keyword also allows you to define rules in your class.
A class created using the abstract keyword is called an abstract class.
Child classes that inherit from abstract classes follow the rules of abstract parent classes.

How to use the abstract keyword for a class

abstract class class name{}

The difference from interfaces is that classes are abstract classes, so you can declare methods. You can also make rules.
Interface + Class = Abstract Class There are also methods in the abstract class that must declare child classes.
Then use the abstract keyword in the method.

How to use the abstract keyword in a method

abstract access modifier function method();

So let's do it.
I'm going to make an abstract class. And create a child class that inherits that abstract class.

<?php
    abstract class Car
    {
        //Child classes must create a run() method.
        abstract public function run();


        //The following method is irrelevant because there is no abstract.
        public static function hello()
        {
            return "Hello? I'm a static method";
        }
    }

    class hello extends Car
    {
        public function disney()
        {
            return 'I Love Disney';
        }
    }

    $hello = new hello;
    echo $hello->disney();
?>

In the code above, the parent class is an abstract class and we use abstract to create a rule that the child class must declare the run() method.
However, the child class broke the parent class's command and did not declare a run() method, which caused the following error. TT
Result of the code above

Result

Now let's listen to the parent class and let the child class declare the run() method.

<?php
    abstract class Car
    {
        //Child classes must create a run() method.
        abstract public function run();


        //The following method is irrelevant because there is no abstract.
        public static function hello()
        {
            return "Hello? I'm a static method";
        }
    }

    class hello extends Car
    {

        public function run()
        {
            return 'Mickey Mouse is running.';
        }

        public function disney()
        {
            return 'I Love Disney';
        }
    }

    $hello = new hello;
    echo $hello->disney();
?>

The code above declares a run() method in a child class. There is no error now and it works fine.
Result of the code above


An abstract class can have child classes call methods of the parent class, as in the following code:

<?php
    abstract class Car
    {
        // Child classes must create a run() method.
        abstract public function run();


        // The following method is irrelevant because there is no abstract.
        public static function hello()
        {
            return "Hello? I'm a method of an abstract class";
        }
    }

    class hello extends Car
    {

        public function run()
        {
            return 'Mickey Mouse is running';
        }

        public function disney()
        {
            return 'I Love Disney';
        }
    }

    $hello = new hello;
    echo $hello->hello();
?>

Result

However, you cannot create an instance of an abstract class.
The following example creates an instance of an abstract class. I get an error

<?php
    abstract class Car
    {
        // Child classes must create a run() method.
        abstract public function run();


        // The following method is irrelevant because there is no abstract.
        public static function hello()
        {
            return "Hello? I'm a method of an abstract class";
        }
    }

    class hello extends Car
    {

        public function run()
        {
            return 'Mickey Mouse is running';
        }

        public function disney()
        {
            return 'I Love Disney';
        }
    }

    $car = new Car;
?>

Result

We have learned how to use the abstract keyword. ^^