Making abstract method parameters final in Java -


i have abstract class abstract method, parameters want final - is, not want allow implementations of abstract class & method reassign parameter.

edit: motivation not immutability per se, more design of objects. (in fact, in use case, parameter collection mutated in implementation of abstract method.) rather, want communicate implementing abstract class/method these variables should not reassigned. know can communicate via java-doc, looking more contractual - have follow, rather guided follow.

in non-abstract method, can using final keyword - example:

public class myclazz {   public void dosomething(final int finalparameter){     finalparameter++; // compile error - cannot assign value final variable   } } 

however, if use final keyword in abstract method, not form part of contract - is, implementations of abstract method not require final keyword, , parameter can reassigned:

public abstract class myabstractclazz {   public abstract void dosomething(final int finalvariable); }  public class myextendedclazz extends myabstractclazz {   @override   public void dosomething(int finalvariable) { // not require final keyword     finalvariable++; // variable modifiable   } } 

as pointed out in answers so question, final keyword not form part of method signature, why implementation of abstract class not require it.

so, there 2 questions:

  1. why final keyword not part of method signature? understand isn't, want know if there's particular reason why isn't.

  2. given final keyword not part of method signature, there alternative way of making parameters in abstract method unassignable?

other research:

  • this so question touches on same issue, doesn't either of 2 questions. in fact, second question explicitly asked, not receive answer.

  • lots of questions/blogs etc. on final keyword refer "the final word". however, respect question, relevant comment follows (which, while useful, doesn't address 2 questions):

note final parameters not considered part of method signature, , ignored compiler when resolving method calls. parameters can declared final (or not) no influence on how method overriden.

i have abstract class abstract method, parameters want final - is, not want allow implementations of abstract class & method reassign parameter.

why not? that's implementation detail. it's unobservable calling code, there's no reason why abstract method should specify it. that's why it's not part of method signature, either - synchronized isn't.

a method should implement documented contract - how chooses it. contract can't useful finality of parameter, java uses pass-by-value.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -