Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does "final" play a role in security? [duplicate]

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?

like image 918
Aan Avatar asked Sep 12 '13 05:09

Aan


People also ask

When a document is marked as final it is saved as this?

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.

How do you unlock a document that is locked for editing?

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.

Why does my Excel file says locked for editing by me?

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.


2 Answers

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.

like image 63
Gaurav Gupta Avatar answered Oct 05 '22 22:10

Gaurav Gupta


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.

like image 25
Juned Ahsan Avatar answered Oct 06 '22 00:10

Juned Ahsan