• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

final & constructors

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic