Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Executing a method in the parent class automatically after the constructor (http://www.velocityreviews.com/forums/t390600-executing-a-method-in-the-parent-class-automatically-after-the-constructor.html)

418928@cepsz.unizar.es 02-04-2007 08:32 PM

Executing a method in the parent class automatically after the constructor
 
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


Eric Sosman 02-04-2007 09:02 PM

Re: Executing a method in the parent class automatically after theconstructor
 
418928@cepsz.unizar.es wrote:
> 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."

--
Eric Sosman
esosman@acm-dot-org.invalid

Alex Hunsley 02-05-2007 12:33 AM

Re: Executing a method in the parent class automatically after theconstructor
 
418928@cepsz.unizar.es wrote:
> 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

buggy 02-09-2007 11:17 PM

Re: Executing a method in the parent class automatically after theconstructor
 
> 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()
{
}
}

----------------------------
If you do not call super(true) then a compile time error will result
"Implicit super constructor MySuper() is undefined"

Daniel Pitts 02-09-2007 11:28 PM

Re: Executing a method in the parent class automatically after the constructor
 
On Feb 4, 12:32 pm, "418...@cepsz.unizar.es" <418...@cepsz.unizar.es>
wrote:
> 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


Mark Rafn 02-10-2007 01:19 AM

Re: Executing a method in the parent class automatically after the constructor
 
418928@cepsz.unizar.es <418928@cepsz.unizar.es> wrote:
>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.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>


All times are GMT. The time now is 07:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.