Describe final class and final method in php

The final keyword is introduced by PHP5 related to the object-oriented programming concept.

But before we move on to the final, we need to ensure that we have a good understanding of the inheritance concept. In inheritance, we can inherit a class from another class. Also, we can override a function in an inherited class, to replace the behavior originally provided. In some cases, we might need to keep a class from being inherited from or we may need to prevent a function to be overridden. This can be achieved with the final, by prefixing the class and function with the final keyword.which essentially causes PHP to produced an error in the event that anybody attempts to extend a final class or override final function.

Note:

we can only use the final keyword with methods and classes.

Let's demonstrate the concept with the help of the below examples.

Example:

We can't override the parent class function.

Output:

PHP Fatal error: Cannot override final method BaseClass::calculate[]

Explanation:

In the above example the class BaseClass which is parent class. In which the calculate method is marked as final. It implies that we cannot override the show method in any of its child classes. To identify the error  ChildClass is trying to define the final method show []. This will produce a fatal error, It implies that the final method of parent class can't be defined in its child class.

Example:

Utilize of "Final" keyword Before The Class


Output:

PHP Fatal error: Class Child may not inherit from final class [BaseClass]

Explanation:

In the above example class BaseClass define with the final, that result we can not extend this class. When class Child tries to extend from BaseClass [ which implies Child is the child class of BaseClass]. For this situation, PHP throws an error message “Class Child may not inherit from the final class [BaseClass]”.It implies that it won't permit creating any child class of BaseClass.

Updated on 30-Jul-2019 22:30:26

  • Related Questions & Answers
  • PHP final Keyword
  • Final class in Java
  • Final keyword in PHP
  • Simulating final class in C++
  • Why Final Class used in Java?
  • Make a class final in Java
  • Is final method inherited in Java?
  • Can a class in Java be both final and abstract?
  • Can a final class be subclassed in Java?
  • What is a final method in Java?
  • final, finally and finalize in C#
  • Private and final methods in C#
  • final, finally and finalize in Java
  • Can a method local inner class access the local final variables in Java?
  • Why String class is immutable or final in Java?

In this article, we will see what is final class & final method in PHP, along with knowing their implementation through the examples.

The terms class, object, method, and final comes under object-oriented programming. In object-oriented programming, there is an important concept called Inheritance that allows one class to inherit properties of another class and even have the capability to override the inherited methods, in order to replace the original behavior given. There are some cases exists where we need to prevent the class from being inherited or from being overridden. For this, the final keyword can be utilized to accomplished  the task in PHP that prevents any class, or method to inherit and override the functionality. It is used simply by prefixing any method or class with the final keyword.

final class: The final class is a class that cannot be extended by other classes. So a Class that is declared with the final keyword doesn’t have child classes. A class can be declared as final by prefixing the final keyword before the Class. The syntax for defining the final class is given below:

Syntax:

final Class className

final method: A method is considered a final method if it is prefixed with the final keyword. The final methods are the methods that cannot be overridden. So, these methods can’t be overridden in child/subclasses. It increases security by preventing the modification of functions. The syntax for defining a final method is given below:

Syntax:

final function functionName[Parameter1, Parameter2, ...];

We will understand these concepts through the below examples.

Example 1: The below example throws an error. Because here the class ParentGFG is declared as final. So any class cannot inherit the classes that are defined as final. So here ChildGFG class cannot inherit the ParentGFG final class. So PHP won’t create a child class for ParentGFG class.

PHP

Output:

Fatal error: Class ChildGFG cannot extend final class ParentGFG in 
            /home/522fb47d933747216c6e5fe0e51ac10d.php on line 11

Example 2: The below example represents that any function defined as final in the parent class cannot be overridden by its child classes. The ParentGFG class has a method print[] which is declared as final. So, whenever any child class of ParentGFG like ChildGFG tries to override the print[] method, PHP throws an error.

PHP

Output:

Fatal error:Cannot override final method ParentGFG::print[] 
            in /home/c055b5cf6357c8cee93036f29c4c2eb8.php on line 11

Advantage of a final class and final method:

  • The advantage of declaring a class or method as final is it increases the security of class and methods by preventing overriding. Both are non-extendable.

Disadvantage of a final class and final method:

  • The disadvantage of declaring a class final is it doesn’t allow to create a child class for that final class thereby decreasing the code reusability.

What is a final method in PHP?

The final keyword is used to prevent a class from being inherited and to prevent inherited method from being overridden.

What is final class in oops?

A final class is a class that can't be extended. Also methods could be declared as final to indicate that cannot be overridden by subclasses. Preventing the class from being subclassed could be particularly useful if you write APIs or libraries and want to avoid being extended to alter base behaviour.

What is called final method?

We can declare a method as final, once you declare a method final it cannot be overridden. So, you cannot modify a final method from a sub class. The main intention of making a method final would be that the content of the method should not be changed by any outsider.

What is final class explain with example?

The final modifier for finalizing the implementations of classes, methods, and variables. The main purpose of using a class being declared as final is to prevent the class from being subclassed. If a class is marked as final then no class can inherit any feature from the final class. You cannot extend a final class.

Chủ Đề