I have read in Wikipedia here that:
A final class cannot be subclassed. This is done for reasons of security and efficiency.
I am wondering about the kind of security that final
in Java can achieve?
When you mark as final, typing, editing commands, proofing marks are disabled or turned off, and the file becomes read-only, and the Status property of the document is set to Final.
To resolve this issue, first confirm that another user on the network does not have the document open. If the document is in use, open the document as read-only. If it is not in use, quit all instances of Word, and then remove the owner file.
If you have locked the file yourself, it might be because the file is open on a different device, or the previous instance of the file didn't close properly. Tip: Sometimes a file may get locked if everyone editing isn't using a version that supports co-authoring.
If your class is final, No one can subclass it. If no one can subclass your final class, that means your features of the final class can not be changed by other by reusing your class.
Assume you are technology provider, here you provide API for some banking system, using your API client will implement its banking system.
case: WITHOUT FINAL
This is in your API
class BaseClass{
public void makeTransaction(){
}
}
This is client code
class DerivedClass extends BaseClass{
public void makeTransaction(){
// naughty client can do something here like:- makeTransaction but transfer 1 dollar for each transaction to my account.
}
}
case: WITH FINAL This is in your API
final class BaseClass{
public void makeTransaction(){
}
}
This is client code
class DerivedClass extends BaseClass{ // not allowed
}
Client has to use make transaction as you have already defined. This is one aspect how final can secure your piece of code.
A final class cannot be subclassed. This is done for reasons of security and efficiency.I am wondering about the kind of security that final in Java can achieve?
If a class cannot be subclassed then you cannot override the functionality that a parent class provides in a child class. From a design perspective you are actually finalizing and enforcing a design using final classes. Hence it makes your final class secured in that sense.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With