interface design question

Discussion in 'Java' started by Rajarshi Guha, Dec 9, 2004.

  1. Hi I have an interface :

    public interface Model {
    public void build() throws ModelException;
    public void predict() throws ModelException;
    }

    Now i have a set of classes which are really specializations of this
    interface. That is all classes will need to implmenent an extra function
    (say initModel()). So I defined a new interface inheriting from the above:

    public interface RModel extends Model {
    public void build() throws ModelException;
    public void predict() throws ModelException;
    Object initModel();
    }

    However what I really want is that initModel() is to *only* be called in
    the constructor of these classes - so ideally initModel() should be
    private. However Java does not allow me to do this.

    Is there a better way to achieve this?

    I thought of creating an base class, say RBase, which will have the above
    methods.

    So it would look like:

    public class RBase implements Model {

    private initModel() {
    // do stuff
    }

    RBase() {
    initModel();
    }

    void build() {
    // do stuff
    }
    void predict() {
    // do stuff
    }
    }

    Then all the other classes would be subclasses of this and their
    constructors would make a call to super().

    However if a subclass calls the constructor of the superclass will
    control be returned to the subclass after this? That is should a call to
    super() be the last statment is a subclasses constructor?
     
    Rajarshi Guha, Dec 9, 2004
    #1
    1. Advertising

  2. Rajarshi Guha

    Yamin Guest

    Rajarshi Guha wrote:
    <snip>
    >
    > Then all the other classes would be subclasses of this and their
    > constructors would make a call to super().
    >
    > However if a subclass calls the constructor of the superclass will
    > control be returned to the subclass after this? That is should a call

    to
    > super() be the last statment is a subclasses constructor?


    On the contrary, a subclass should call super() as the first statement
    in its constructor. Since the subclass depends on the base class would
    it not make sense to initialize the base class first?
    The baseclass method is probably the best way to go.

    Yamin
     
    Yamin, Dec 9, 2004
    #2
    1. Advertising

  3. Rajarshi Guha

    Guest

    A call to a superclass constructor must occur as the first executable
    line of a subclass constructor. If no call is made, then implicilty
    super() is called. The call to the superclass constructor initializes
    the portion of the subclass which is inherited, and the remainder of
    the subclass constructor will initialize the variables that have added
    to the subclass.
     
    , Dec 9, 2004
    #3
  4. Exactly. And note that you dont need to call super() if you have no
    arguments.
     
    sanjay manohar, Dec 9, 2004
    #4
  5. On Thu, 09 Dec 2004 13:29:09 -0500, Rajarshi Guha wrote:

    > Hi I have an interface :
    >
    > public interface Model {
    > public void build() throws ModelException;
    > public void predict() throws ModelException;
    > }
    >
    > Now i have a set of classes which are really specializations of this
    > interface. That is all classes will need to implmenent an extra function
    > (say initModel()). So I defined a new interface inheriting from the above:
    >
    > public interface RModel extends Model {
    > public void build() throws ModelException;
    > public void predict() throws ModelException;
    > Object initModel();
    > }
    >


    Thanks for the pointers
     
    Rajarshi Guha, Dec 9, 2004
    #5
  6. In article <>, Rajarshi
    Guha () dropped a +5 bundle of words...

    > Hi I have an interface :
    >
    > public interface Model {
    > public void build() throws ModelException;
    > public void predict() throws ModelException;
    > }
    >
    > Now i have a set of classes which are really specializations of this
    > interface. That is all classes will need to implmenent an extra function
    > (say initModel()). So I defined a new interface inheriting from the above:
    >
    > public interface RModel extends Model {
    > public void build() throws ModelException;
    > public void predict() throws ModelException;
    > Object initModel();
    > }
    >
    > However what I really want is that initModel() is to *only* be called in
    > the constructor of these classes - so ideally initModel() should be
    > private. However Java does not allow me to do this.


    No. You have to make a call to the superclass constructor too. Make the
    same call as you would to the subclass constructor.

    Public Blah(String name) {
    super(name)
    // YOU MUST HAVE THIS LINE BEFORE ANY OTHER STATEMENTS
    // IN THE CONSTRUCTOR

    }

    --
    Starshine Moonbeam
    mhm31x9 Smeeter#29 WSD#30
    sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM
     
    Starshine Moonbeam, Dec 9, 2004
    #6
  7. Rajarshi Guha

    Eric Sosman Guest

    Rajarshi Guha wrote:
    > Hi I have an interface :
    >
    > public interface Model {
    > public void build() throws ModelException;
    > public void predict() throws ModelException;
    > }
    >
    > Now i have a set of classes which are really specializations of this
    > interface. That is all classes will need to implmenent an extra function
    > (say initModel()). So I defined a new interface inheriting from the above:
    >
    > public interface RModel extends Model {
    > public void build() throws ModelException;
    > public void predict() throws ModelException;
    > Object initModel();
    > }
    >
    > However what I really want is that initModel() is to *only* be called in
    > the constructor of these classes - so ideally initModel() should be
    > private. However Java does not allow me to do this.
    >
    > Is there a better way to achieve this? [...]


    Others have addressed your question about super(), but
    I think your difficulty with initModel() is self-inflicted:
    if it's a private part of the implementation, don't publish
    it: leave it out of the interface altogether.

    True, this will mean that you cannot force every subclass
    to provide an initModel() method -- but if the method is only
    intended to be called from inside the class' own constructors,
    who cares? You aren't going to call the method from anywhere
    else, so you don't need to know whether it exists or not. Take
    it out of the interface and make it private -- simple as that.

    --
     
    Eric Sosman, Dec 9, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wildepiet
    Replies:
    0
    Views:
    1,888
    Wildepiet
    Jun 14, 2004
  2. Martha Goys

    Interface design question

    Martha Goys, Oct 26, 2004, in forum: Java
    Replies:
    5
    Views:
    372
    Martha Goys
    Oct 28, 2004
  3. Michael De Tomaso

    Interface Design Question

    Michael De Tomaso, Mar 10, 2005, in forum: HTML
    Replies:
    3
    Views:
    402
    KHaled
    Mar 17, 2005
  4. MK
    Replies:
    0
    Views:
    417
  5. Replies:
    4
    Views:
    337
    Öö Tiib
    Oct 19, 2012
Loading...

Share This Page