interface design question

R

Rajarshi Guha

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?
 
Y

Yamin

Rajarshi Guha wrote:
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
 
K

klynn47

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.
 
S

sanjay manohar

Exactly. And note that you dont need to call super() if you have no
arguments.
 
R

Rajarshi Guha

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
 
S

Starshine Moonbeam

Rajarshi said:
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

}
 
E

Eric Sosman

Rajarshi said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top