• Post Reply Bookmark Topic Watch Topic
  • New Topic

Way to force the methods to be implemented by user  RSS feed

 
Milton Singh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Please help me with the follwing problem:
> I have my own utility class, all methods are static there .
> Don't want to create object for that class
> Any other class can refer that utility class mehtods directly as both are in same package
> But I want to make sure that all those methods must be implemented.
> There is some infrastructure design constarint that is why i can't mark my utility class as base class
> I cant use the interface also because of this infrastructure thing.

Hope You get my requirement:

In brief I want to force other classes to use the methods of my utility class without extending it.

Thanks,
Milton
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In brief I want to force other classes to use the methods of my utility class without extending it.


1. NOT a performance question
2. read about use of the final modifier with classes, see the java.lang.String class for example

Bill
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:
1. NOT a performance question

Agreed. Moving to Java in General.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a utility class, give it a private constructor, no getInstance() method, and mark the class final. All the methods must be fully implemented in this class.

You cannot "force" users to implement a method any way at all (but that statement implies an interface or abstract class), but you can insist in the documentation comments that the methods must express a certain behaviour. You can be prescriptive (see Object#equals) or you can be vague (see Collection#add).

But what you have appears already to express the behaviour you wanted.
A final class cannot be inherited from (similarly if it only has a private constructor), so all its methods must be concrete, and static methods have to be concrete too, and cannot be overridden.
[ September 20, 2008: Message edited by: Campbell Ritchie ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!