What is the meaning of a final class and a final method

The Eduladder is a community of students, teachers, and programmers just interested to make you pass any exams. So we solve previous year question papers for you.
In eduladder you can Ask,Answer,Listen,Earn and Download Questions and Question papers.
Watch related videos of your favorite subject.
Connect with students from different parts of the world.
Apply or Post Jobs, Courses ,Internships and Volunteering opportunity. For FREE
See Our team
Wondering how we keep quality?
Got unsolved questions? Ask Questions

PHP-->View question

What is the meaning of a final class and a final method?

its about What is the meaning of a final class and a final method


Asked On2019-09-06 15:46:36 by:Annaliya

Taged users:


Likes:
Be first to like this question

Dislikes:
Be first to dislike this question
Talk about this  Like  Dislike
View all qusetions
Answers

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.

<?php
   class BaseClass{
      final function calculate($val1,$val2){
         $sum = $val1+$val2;
         echo "Sum of given no=".$sum;
         }
      }
   class ChildClass extends BaseClass{
      function calculate($x,$y){
         $mult=$val1*$val2;
         echo "Multiplication of given no=".$mult;
         }
      }
   $obj= new ChildClass();
   $obj->show(10,10);
?>

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

<?php
   final Class BaseClass{
      function printData($val1,$val2){
         $add=$val1+$val2;
         echo "Sum of given no=".$s;
         }
      }
   class Child extends BaseClass{
      function printData($val1,$val2){
         $m=$val1*$val2;
         echo "Multiplication of given no=".$m;
         }
      }
   $obj= new Child();
   $obj->printData(20,20);
?>

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.


Answerd on:2019-09-09 Answerd By:Tanisha-Garg

Likes:
Be first to like this answer

Dislikes:
Be first to dislike this answer
Talk about this  Like  Dislike

You might like this video:Network and IP Address
Watch more here

Watch more videos from this user Here

Learn how to upload a video and start earning here

Inheritance is surely one of the highly useful features in Java. But at times, it may be desired that a class should not be extendable by other classes to prevent exploitation. For such purpose, we have the final keyword. We have already seen even what final variables are. Final classes and methods are also similar. A class declared as final cannot be extended while a method declared as final cannot be overridden in its subclasses. A method or a class is declared to be final using the final keyword. Though a final class cannot be extended, it can extend other classes. In simpler words, a final class can be a sub class but not a super class. 

final public class A {
    //code
}

The final keyword can be placed either before or after the access specifier. The following declaration of class A is equivalent to the above. 

public final class A {
    //code
}

To make it more clear, there should be no specifiers between a class name and the keyword class. All other specifiers may be placed to the left of the keyword class in any order. 

Final methods are also declared in a similar way. Here too, there should be no specifiers stated between the return type and the method name. For example, to make a static method declared with the public access specifier final, the three specifiers, public, static and final may be placed in any order to the left of the return type. 

public final void someMethod() {
    //code
}

When we attempt to extend a final class or override a final method, compilation errors occur. 

class B extends A { // compilation error, A is final
}

We will now look into a small trivial example to understand the necessity of final classes and methods. Consider that you are a developer writing classes for use by others. You have two classes named Car and Vehicle defined in the following way and of course with may other methods of your own. 

public class Car {

    public void move() {
        System.out.println("Moving on road");
    }
}

class MoveAVehicle {

    public static void move(Car c) {
        c.move();
    }
}

Now, since the class Car is not final, other people may extend it to their own classes. Following is an example: 

class Aeroplane extends Car {

    public void move() {
        System.out.println("Moving in air");
    }
}

This is logically wrong. An aeroplane is definitely not a Car. And a statement like the following would print absurd data on the screen.

MoveAVehicle( new Aeroplane() );

In order to avoid such illogical extending of classes; classes and methods may be marked as final. The example cited above is just illustrative. The actual scenario in real world programming is much more complex. 

Answerd on:2019-09-09 Answerd By:Tejasurve

Likes:
Be first to like this answer

Dislikes:
Be first to dislike this answer
Talk about this  Like  Dislike

You might like this video:Javascript
Watch more here

Watch more videos from this user Here

Learn how to upload a video and start earning here



Lets together make the web is a better place

We made eduladder by keeping the ideology of building a supermarket of all the educational material available under one roof. We are doing it with the help of individual contributors like you, interns and employees. So the resources you are looking for can be easily available and accessible also with the freedom of remix reuse and reshare our content under the terms of creative commons license with attribution required close.

You can also contribute to our vision of "Helping student to pass any exams" with these.
Answer a question: You can answer the questions not yet answered in eduladder.How to answer a question
Career: Work or do your internship with us.Work with us
Create a video: You can teach anything and everything each video should be less than five minutes should cover the idea less than five min.How to upload a video on eduladder