• Post Reply Bookmark Topic Watch Topic
  • New Topic

final & constructors  RSS feed

 
Matthew Alesi
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that constructors cannot be marked final because they can never be inherited and overridden, and so it is redundant. However, you can declare an interface as abstract even though it's obvious that it is, and you can declare a private method as final even though it too will never be inherited. So my question is, why is there a special exception to declaring constructors final?
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces are abstract, so while adding the "abstract" keyword to the definition is redundant it's still correct. As you mentioned, constructors can't be overridden, so using the "abstract" keyword on a constructor definition isn't just redundant, it's just plain incorrect, ergo the compile-time error.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nathaniel Stoddard:
Interfaces are abstract, so while adding the "abstract" keyword to the definition is redundant it's still correct. As you mentioned, constructors can't be overridden, so using the "abstract" keyword on a constructor definition isn't just redundant, it's just plain incorrect, ergo the compile-time error.


Not at all what they asked.

While it would be redundant to declare an interface abstract or a private method final I would venture a guess that the author's simply didn't bother to make them explicitly compile-time errors.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would seem the answer to your question is, 'that is how it was designed'.

From the Java Language Specification:

8.8.3 Constructor Modifiers
.
.
.
Unlike methods, a constructor cannot be abstract, static, final, native, strictfp, or synchronized.


9.1.1.1 abstract Interfaces
Every interface is implicitly abstract. This modifier is obsolete and should not be used in new programs.


8.4.3.3 final Methods
A private method [is] implicitly final, because it is impossible to override [it]. It is permitted but not required for the declarations of such methods to redundantly include the final keyword.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!