• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final classes in Java

 
swarupa patil
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why String Math System and some other classes are Final ?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The are two different reasons at play here:

1. String being final is part of String's design as an immutable class.

2. System and Math are utility classes with only static methods. There are not meant to be instantiated (and that's why their constructors are private). To emphasize the fact they are not meant to be instantiated, they are marked as final, although the lack of an accessible constructor would have stopped you anyway.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've always thought it's a shame that Java does not allow you to declare a class "abstract final". Then you could avoid defining any constructor at all.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I think "abstract" has the strong connotation of "built for subclassing", so that's why the combination "final+abstract" is illegal.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic