Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Forcing people to implement static methods

 
Tony Smith
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Tony Smith
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me it sounds like a work for the Inversion of Control Pattern.

What do you, guys, think?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic