Link to home
Start Free TrialLog in
Avatar of _lychee_
_lychee_

asked on

Forcing subclasses to perform tasks

Is there any way I can force subclasses to perform stuff at class initialisation?

re-statement:

I want my subclasses to call a particular method to register themselves with a factory; is there any way of doing this?
Avatar of BillyAbbott
BillyAbbott
Flag of United Kingdom of Great Britain and Northern Ireland image

if you mean when you create a an object of the superclasses type, then no as far as i know.

if you want to put in a method that forces all subclasses to register, put a call to it in the superclass constructor, and then call super(); as the first line of your subclass cinstructor.

is that waht you mean?
Avatar of _lychee_
_lychee_

ASKER

no...
my class is an abstract class... i want my class to FORCE subclasses to do certain things when they initialise... ie. the same time that a static initialisation block executes
Do you want to put a reference to the CLASS object into the factory on itialisation of the class for the first time? I'm not sure what it is exactly you need to do.

This may also help. When creating a class the order of initialisation is (from thinking in Java):

1. The first time an object of type Dog is created, or the first time a static method or static
field of class Dog is accessed, the Java interpreter must locate Dog.class, which it does
by searching through the classpath.

2. As Dog.class is loaded (which creates a Class object, which you’ll learn about later), all
of its static initializers are run. Thus, static initialization takes place only once, as the
Class object is loaded for the first time.

3. When you create a new Dog( ), the construction process for a Dog object first allocates
enough storage for a Dog object on the heap.

4. This storage is wiped to zero, automatically setting all the primitives in Dog to their
default values (zero for numbers and the equivalent for boolean and char).

5. Any initializations that occur at the point of field definition are executed.

6. Constructors are executed. As you shall see in Chapter 6, this might actually involve a
fair amount of activity, especially when inheritance is involved.


So, any static method which you access in the class will force all static initializers to run, so you can put a relevant static method into your class which will inherently be in your subclasses. Is this what you mean?

i don't really get ur comment... ok... here's the take on the situation:

i have a class that defines operators (as in math operators)

abstract public class Operator {
   ...
}

and i have a class that generates Operator-s based on tokens, say it's called OperatorFactory

public final class OperatorFactory {
   ...
}

i want all subclasses of Operator to register themselves with the OperatorFactory... i know this can be done by putting some code into every subclass of Operator, but is there any way that Operator can ensure that its subclasses do it?

the registration method is something like

public void registerOperator(Operator boo, String tok) {...}
and is in OperatorFactory (of course)
oh yar...
OperatorFactory is a singleton; don't worry about the registerOperator method not being static and stuff like that...
OH NO... i see a problem...
ok it won't work... argh... cos i can't get the class to initialise... is that right?
ok i'm gonna delete this qn and post another one regarding something similar.
This question has a deletion request Pending
This question no longer is pending deletion
The problem is the static initialiser will only run once - so anything static is probably not going to achieve what you want because it cannot re-initialise for each type of subclass without having additional static code specific to each subclass.

Make sense?

So the static initialisation for the operator class will only run once unless you have more static code to do in each subclass - and you can't force these subclasses to include this.
You could write your own class loader to do it....:)
Why do you need to have your subclasses bind to the factory themselves?

could you not do this the other way around and return an operator from the operatorfactory but decide which class to instantiate based on the string name of the class passed in.

Then you can use reflection to instantiate the relevant class.
i didn't want to hardcode the operators in cos i wanted the user to be able to add operators which my proggy would then find and add automatically... but since that's not possible i have decided to use an initialisation file to determine the classes to load in...
ASKER CERTIFIED SOLUTION
Avatar of Jod
Jod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Community Support has reduced points to 100