Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Executing a method in the parent class automatically after the constructor

Reply
Thread Tools

Executing a method in the parent class automatically after the constructor

 
 
418928@cepsz.unizar.es
Guest
Posts: n/a
 
      02-04-2007
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

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      02-04-2007
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
lid
 
Reply With Quote
 
 
 
 
Alex Hunsley
Guest
Posts: n/a
 
      02-05-2007
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
 
Reply With Quote
 
buggy
Guest
Posts: n/a
 
      02-09-2007
> 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"
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      02-09-2007
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

 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      02-10-2007
<> 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 <http://www.dagon.net/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Constructor question, how does the call to the parent constructor work? marcwentink@hotmail.com C++ 6 05-09-2006 07:19 AM
Invoking templatized base class constructor from templatized derived class constructor mrstephengross C++ 5 05-18-2005 07:12 PM
Calling parent constructor from child constructor pantalaimon C++ 3 10-09-2004 09:17 AM



Advertisments