• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

abstract final classes

 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"?
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a much better answer...
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
My cellmate was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic