views:

74

answers:

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

A: 

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.

alex
please elaborate...
OM The Eternity
+2  A: 

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
But why ?? There has to be any specific reason like wise we have for access specifiers...
OM The Eternity
+3  A: 

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
"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
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
Thanks It helped me.... :)
OM The Eternity
+1  A: 

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