Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract final classes  RSS feed

 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know they're not allowed, but what if you wanted to create a class that just included static methods and data... like the Math class.

You'd never want to create an instance and I can't think why you should have to be able to extend it.

Is there a good reason why they aren't allowed in combination, other than "it's not very OO"?
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps because abstract means "you have to extend this to use it", and final means "you can' t extend this". Somebody trying to create this is much more likely making a mistake than anything useful, and allowing them to be used together for something doesn't make any sense.

If you really must create a class with static methods that you never want instantiated, have its constructor throw an exception. Make sure it's a runtime exception, and make sure it's your own exception, something like CompleteWeirdoException.

I will leave it to you to come up with a reason why you would worry about someone instantiating something that had no instance methods or variables.

rc
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralph Cook wrote:...If you really must create a class with static methods that you never want instantiated, have its constructor throw an exception...

Or just make the class final with a private constructor, like the Math class.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a much better answer...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An abstract class is like a "rough sketch" of what the completed class might look like. It's a good start, but it's incomplete, leaving the final details to the derived (concrete) class.

A class that's not intended to be instantiated, but instead supplies static methods, is more of a "utility" class. (See Wikipedia - Utility Pattern.)
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, it seems to be a slight misunderstanding on my part of what an abstract class is supposed to be. If you don't want a class to be instantiated just make the constructor private. Only mark the class abstract if it's a template to be extended, rather than because you don't want it to be instantiated.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luigi Plinge wrote:OK, it seems to be a slight misunderstanding on my part of what an abstract class is supposed to be. If you don't want a class to be instantiated just make the constructor private. Only mark the class abstract if it's a template to be extended, rather than because you don't want it to be instantiated.

That's exactly right!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!