| Author |
final & constructors
|
Matthew Alesi
Ranch Hand
Joined: Sep 13, 2006
Posts: 38
|
|
|
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?
|
-Matt
Current CS undergrad
SCJP 5.0
|
 |
Nathaniel Stoddard
Ranch Hand
Joined: May 29, 2003
Posts: 1258
|
|
|
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.
|
Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
|
 |
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
|
|
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
Joined: Aug 05, 2005
Posts: 3011
|
|
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.
|
Joanne
|
 |
 |
|
|
subject: final & constructors
|
|
|