happy cat image

everdevel

coding

login
NotificationX
  • Currently, only notices of comments are displayed.
  • no comment or please login

Preventing overriding

Let's see how to disable overriding.
Very simple. Use the final keyword before any accessor restrictions on methods that will prevent overriding.

How to use final in methods

final access modifier function method name(){}

Let's use the overriding example we learned earlier and add the final keyword.

<?php

    class GoogleCar
    {
        final public function stateOfTheArtAIDrivingSystem()
        {
             return "Google AI Driving" ;
        }
    }

    class Car extends GoogleCar
    {
        public $wheels;
        public $doors = 4;
        protected $color = 4;
        private $size;
        private $company;

        public function stateOfTheArtAIDrivingSystem()
        {
            return "AI driving, which adds functionality to existing Google AI driving, uses IOT to connect to all vehicles Blah Blah ~~~~";
        }
    }
    $ever = new Car;
    echo $ever->stateOfTheArtAIDrivingSystem();
?>

Result

I've disabled overriding using the final keyword, but when I call those methods, I get an error.
^^ We learned about the final keyword.
The final keyword can also be used for other purposes.
Used in front of the class has another function. See you next time.


Thank you for visiting. If you have any inquiry or explanation of mistakes, please use the comments below.


    
    

Back to the course

ALL COMMENTS 0

Sort by