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