aspose file tools*
The moose likes Java in General and the fly likes Forcing people to implement static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Forcing people to implement static methods" Watch "Forcing people to implement static methods" New topic
Author

Forcing people to implement static methods

Tony Smith
Ranch Hand

Joined: Mar 15, 2005
Posts: 77
Assume base class A and subclass X.

I would like to be able to create classes and retain a little more control:



with



and in A:



The idea is that each of the subclasses has an explicit method that will create an instance but the creation is controlled by A.

For this to work, however, I need to force all subclasses to implement
'static X newInstance();' But I don't see a way to do this. 'static' isn't allowed in interfaces and I can't seem to make a 'static abstract' method in A.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14164
    
  19

Static methods are not inherited, so you can't make an abstract static method in an abstract base class or interface to force people who write a non-abstract subclass to implement that static method.

Also, you can't prevent people from implementing a public constructor in a subclass, with which they could circumvent your static factory method mechanism.

I can't think of any way to force people to use such a mechanism.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Static methods cannot be inherited, because they belong to the class. Therefore there is no way for your class to force the implementation of a static method.

However, you could make the constructor private or protected, forcing the instantiation of object through a static method you have provided.

This method may have the same name as that you define in the base class, however, you must be really careful and understand that this is in no way an inheritance mechanism. You would simple be hiding the method of the base class with one define in the child class.

Therefore if you do somewhat like this:


In the following code:


Therefore if you ever do this, try to access the static methods using the class identifier and not a particular instance of it, because you can get ackward behavior difficult to spot and debug.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14164
    
  19

Edwin: "However, you could make the constructor private or protected, forcing the instantiation of object through a static method you have provided."

But this still does not stop anybody who writes a subclass from adding a public constructor to the subclass to circumvent the static factory method.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Jesper Young:

But this still does not stop anybody who writes a subclass


If the constructor is private, then, of course, no one can define a subclass in the first place!


[Jess in Action][AskingGoodQuestions]
Tony Smith
Ranch Hand

Joined: Mar 15, 2005
Posts: 77
This sounds like a bad idea. The syntax of the language doesn't support what I am trying to accomplish.

I think I'll go with plan B:



This allows me to control the creation of the instance while simultaneously making a single place where the creation happens. Now if I needed to add security, object caching :roll: , or some other higher level management I could do it.

Why? It's a compromise.
[ October 18, 2006: Message edited by: Tony Smith ]
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
To me it sounds like a work for the Inversion of Control Pattern.

What do you, guys, think?
 
wood burning stoves
 
subject: Forcing people to implement static methods