Executing a method in the parent class automatically after the constructor

4

418928

Hi everybody,

I don't know if there is any trick for this. I have a class A and
several classes that inherit from A. Each of these classes should call
always a certain method on its parent A after the constructor has been
called (i.e., after the class is fully initialized). Is there any way
to force this automatically? Perhaps using a factory... but I would
like not to touch the existing code regarding the creation of the
instances of the subclasses...

Thanks,
Sergio
 
E

Eric Sosman

Hi everybody,

I don't know if there is any trick for this. I have a class A and
several classes that inherit from A. Each of these classes should call
always a certain method on its parent A after the constructor has been
called (i.e., after the class is fully initialized). Is there any way
to force this automatically? Perhaps using a factory... but I would
like not to touch the existing code regarding the creation of the
instances of the subclasses...

I don't think there's a way to do this -- certainly not
if the subclasses have accessible constructors.

It seems a peculiar thing to want to do, though. Methods
in class A are "blind" to the natures of A's subclasses, so
there's not much an A method can do that makes effective use
of subclass-specific features; A's method might just as well
run at the end of A's own constructor. Perhaps you want the
A method to call a method the subclass is expected to override?
Well, that seems peculiar, too: It means A is in a sense privy
to the implementation details of its subclasses, a notion that
runs counter to the idea of encapsulation ...

Perhaps if you'd describe the larger problem you're trying
to solve, someone might suggest a way to solve it without the
need for "constructor callbacks."
 
A

Alex Hunsley

Hi everybody,

I don't know if there is any trick for this. I have a class A and
several classes that inherit from A. Each of these classes should call
always a certain method on its parent A after the constructor has been
called (i.e., after the class is fully initialized). Is there any way
to force this automatically?

No. Why do you want to do this? It sounds to me, perhaps, that you want
to 'fix' the fact that a subclass isn't compelled to call the super
class's constructor by enforcing some setup code elsewhere.... (of
course I may be completely wrong about this!) As Eric asked: what are
you trying to achieve?

Anyway, the fact the you're thinking along these lines indicates to me
that there is something not quite right with the design you have in mind.
It's quite acceptable when designing a class to specify a contract in
the documentation along the lines of 'after constructing this class, you
must call method b() before calling any other methods (e.g. c(), d(), or
e()), or otherwise an exception/undesirable outcome will occur". This
way, you're not *forcing* something to happen, but you're also making it
known (in a reasonable way) what the expected/acceptable interaction
with your class is.


A word of warning - in case you're tempted to put extra shenanigans at
the end of the constructor itself: any constructor that calls a method
that is non-static, non-final (or non-private) may provide you with some
odd-seeming surprises later.

To demonstrate:

Q: What does the code below output to the screen? (Please don't compile
and run this - just answer by reading the code.)

class A {

public A() {
setUpSomeThings();
}

protected void setUpSomeThings() {
// some setup code in here
}

}

public class B extends A {
private int memberVariable = 0;

public B() {
super();

System.out.println("memberVariable = "
+ memberVariable);
}

protected void setUpSomeThings() {
memberVariable = 7;
}

public static void main(String[] args) {
B bInstance = new B();
}

}

Bonus question: What is the output if we change the line:

private int memberVariable = 0;


For reasons demonstrated by the code above, constructors should be kept
as simple as possible. Constructors that have a side effect (such as
showing a window, or generally performing any action (a verb!) in any
higher level sense) are to be avoided.

A constructor generally constructs (sets up) a *thing* (an object - or
think 'noun') -- and any actions (verbs) that that thing will do should
usually be triggered by calling a method on that object.
Perhaps using a factory... but I would
like not to touch the existing code regarding the creation of the
ins

A factory may help - but again, what is the situation you're dealing
with here?
lex
 
B

buggy

It sounds to me, perhaps, that you want to 'fix' the fact that a
> subclass isn't compelled to call the super class's constructor

Sure you can compel a call to the super's constructor. Don't declare a
class constructor with an empty parameter.

----------------------------
public abstract class MySuper()
{
public MySuper(boolean dummy)
{
super();
// do some special stuff
}
}

class MuSub extends MySuper()
{
public MySub()
{
}
}
 
D

Daniel Pitts

Hi everybody,

I don't know if there is any trick for this. I have a class A and
several classes that inherit from A. Each of these classes should call
always a certain method on its parent A after the constructor has been
called (i.e., after the class is fully initialized). Is there any way
to force this automatically? Perhaps using a factory... but I would
like not to touch the existing code regarding the creation of the
instances of the subclasses...

Thanks,
Sergio

It sounds like you want to use aspect oriented programming.
I've never really worked with it, but I've heard of ways to do it.

Look into AspectJ
 
M

Mark Rafn

I don't know if there is any trick for this. I have a class A and
several classes that inherit from A. Each of these classes should call
always a certain method on its parent A after the constructor has been
called (i.e., after the class is fully initialized). Is there any way
to force this automatically?

No, aside from having methods in A that throw IllegalStateException if the
class isn't properly initialized, and documenting the heck out of it.

Or you could move the required configuration into A's constructor, which will
then be executed BEFORE initialization of the subclass. This is the cleanest
way.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top