4

What Could be the example scenarios to declare any class or any method as "final"? When do we need to do this?

Please give me some example to understand the practical use of "final"...

please elaborate your answer....

please guide me as I am a beginner in OOP

alex
  • 479,566
  • 201
  • 878
  • 984
OM The Eternity
  • 15,694
  • 44
  • 120
  • 182
  • 1
    to avoid clients extending and modifying the class/method. you might find this discussion interesting around why Strings are final in Java - http://stackoverflow.com/questions/2068804/why-is-string-final-in-java – Anurag Apr 23 '10 at 05:12
  • @Anurag the points regarding the "immutability" and other such sophisticated terminology are out of scope of my Mind :) can u please can u elaborate it anyway? – OM The Eternity Apr 23 '10 at 05:19
  • posted an answer with a basic example. – Anurag Apr 23 '10 at 05:27

4 Answers4

5

This is just an example to elaborate why methods/classes must be locked down sometimes from any further extensions to avoid surprises.

class Account {
    public function debit(Amount $amount) {
        if(($this->canDebit($amount)) {
            $this->balance -= $amount;
        }
    }

    final public function credit(Amount $amount) {
        ...
    }
}

class CheckingAccount extends Account {
    public function debit(Amount $amount) {
        // we forgot to check if user canDebit()
        $this->balance -= $amount;
    }
}

This is just an example to illustrate why it's necessary to make methods and classes final. In fact, it is a very good practice to make all your methods and classes final unless you know you are going to extend them, and the subclasses may want (and will be allowed) to extend/override the base class.

Anurag
  • 140,337
  • 36
  • 221
  • 257
  • "it is a very good practice to make all your methods and classes final unless you know you are going to extend them, and the subclasses may want (and will be allowed) to extend/override the base classes." I will follow this... I understood the example, But I Have a doubt that is this also done for the security purpose? If Yes then Y, why is it necessary to secure it? – OM The Eternity Apr 23 '10 at 05:30
  • for the same reasons.. this example above may have been an innocent mistake, but there could be malicious classes as well.. read up this section 1.2 for more info - http://java.sun.com/security/seccodeguide.html#1-2 – Anurag Apr 23 '10 at 05:42
2

Basically you declare any class final when you think it should not be extended any more or there is no need for either you or anyone else to extend that class.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
1

Classes are designed with the intent of being extended or without the intent of being extended. If your class has no intent of being a parent it should be stated as final. A class designed to be extended has the responsibility to lay the framework from which the child will work from. Any methods that define the rules of the framework should be stated as final.

This description is as vague as the question.

I like the accounting example from Anurag and I wish to show how the correct usage.

abstract class Account {

    // obtained by some magical source
    private $balance = 100.00;

    final public function getBalance() {
        return $this->balance;
    }

    final private function setBalance($new_balance) {
        $this->balance = $new_balance;
    }

    final public function debit(Amount $amount) {
        if ($this->canDebit($amount)) {
            $amount = $amount + $this->getDebitTransactionFee();
            $this->setBalance($this->getBalance() - $amount);
        }
    }

    abstract protected function canDebit();
    abstract protected function getDebitTransactionFee();

}

final class CheckingAccount extends Account {

    final protected function canDebit() {
        return true;
    }

    final protected function getDebitTransactionFee() {
        // obtained by some magical source
        return 1.50;
    }

}

The responsibility of Account is to keep track of the balance, allow the public to debit, and allow the public to check the current balance. The responsibility of CheckingAccount is to answer if it can be debited and report on the respective transaction fee. Obviously the accounting here is extremely simplified. This example is one of likely infinite situations.

Abstract classes are quite common for classes meant to be extended. Sometimes, however, a class can define its own non-final methods with a default function and have an operational instance. Of course, being non-final, these default methods are then free to be overridden by a child class.

erisco
  • 14,154
  • 2
  • 40
  • 45
0

For a static class only, you could do

final private function __construct()
{
// This is a static class
}

Like how Kohana does it.

You could also decide that this class (which extended another) is the last you'd like. Using the final keyword will hint to future developers your intention.

So if they try and extend it, they will get an error and know why they couldn't.

Valery Viktorovsky
  • 6,487
  • 3
  • 39
  • 47
alex
  • 479,566
  • 201
  • 878
  • 984