• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Forcing people to implement static methods

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To me it sounds like a work for the Inversion of Control Pattern.

What do you, guys, think?
 
Did you miss me? Did you miss this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic