This discussion is archived
5 Replies Latest reply: Feb 21, 2007 5:23 AM by 807606 RSS

Blocking constructors

807606 Newbie
Currently Being Moderated
Hi,

I'm working on a framework and in this framework I've written a class with some abstract and concrete methods.
Every program other programmers are making with this framework must have a class extending my class. They are allowed to use 4 different constructors (in which they can write their own code), but I would like to prevent them from creating their own constructors. Can this be achieved in Java ?
At the moment, we're working with JDK 1.5.0 and Eclipse 3.2.
// Function is a class every program must have
// GUIFunctionContext is the class I've written in the framework
public class Function extends GUIFunctionContext {
    // The 4 constructors that are allowed
    public Function(GUIFunctionContext parent) {
        super(parent);
    }

    public Function(GUIFunctionContext parent,Parameters parameters) {
        super(parent, parameters);
    }
    
    public Function(ApplicationManager applicationManager) {
        super(applicationManager);
    }

    public Function(ApplicationManager applicationManager,Parameters parameters) {
        super(applicationManager, parameters);
    }

    // Constructor that needs to be disallowed
    public Function(String text1, String text2) {
    ...
    }
}
  • 1. Re: Blocking constructors
    807606 Newbie
    Currently Being Moderated
    Why is it public?
    public Function(String text1, String text2) 
  • 2. Re: Blocking constructors
    791266 Explorer
    Currently Being Moderated
    What's the reason that you don't want them to let them implement constructors? They must call one of yours even if they create one.

    Kaj
  • 3. Re: Blocking constructors
    807606 Newbie
    Currently Being Moderated
    Well if tht must extend your class then they must call 1 of the 4 constructors you made. But if they will be allowed to alter your class then you can't tell java to not allow it(at least not as far as i know).

    Maybe i'm missing the point but if you provide the function class with the allowed cocstructors why would they modify it?

    Lima
  • 4. Re: Blocking constructors
    807606 Newbie
    Currently Being Moderated
    The class Function is called by the framework when the whole application is started.
    First the framework starts with a MDI-frame and a JTree serving as menu. When the end-user clicks on an item in the tree,a specific program is launched. To enable the framework to launch the program and handling the communication with our As400-servers for that program, the class Function is necessary. The superclass GUIFunctionContext takes care for that communication.
    One of the main reasons to prevent the other programmers creating other constructors is because they do create other constructors and when it doesn't work (because the framework don't care about the other constructors) they're standing at my desk :-(
    When they us it normally, they'll make a private method in Function which is called in each constructor. In this method, they set all the providers needed to store the data coming from AS400.
  • 5. Re: Blocking constructors
    807606 Newbie
    Currently Being Moderated
    This is why almost all frameworks assume a no-args constructor and use some kind of init method to do the actual initialization.

    On the other hand you need to supply sensible configuration error messages for other inconsistencies relating to the plugin, so why not this one?