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?
LVL 2
_lychee_Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BillyAbbottCommented:
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?
0
_lychee_Author Commented:
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
0
JodCommented:
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?

0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

_lychee_Author Commented:
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)
0
_lychee_Author Commented:
oh yar...
OperatorFactory is a singleton; don't worry about the registerOperator method not being static and stuff like that...
0
_lychee_Author Commented:
OH NO... i see a problem...
ok it won't work... argh... cos i can't get the class to initialise... is that right?
0
_lychee_Author Commented:
ok i'm gonna delete this qn and post another one regarding something similar.
0
_lychee_Author Commented:
This question has a deletion request Pending
0
JodCommented:
This question no longer is pending deletion
0
JodCommented:
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.
0
JodCommented:
You could write your own class loader to do it....:)
0
JodCommented:
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.
0
_lychee_Author Commented:
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...
0
JodCommented:
Yep, probably as good a way as any.

Just an additional thought though which is maybe a bit over the top but you can consider how it might help...

I was thinking of implementing your factory using RTTI. The user does not register their object with the factory but the factory creates the object from the name and constructor parameters passsed in.

EG:

public class OperatorFactory {

  ....
  public static Operator create(String operatorClass, Object[] parameters) {
    ....
  }
  ....
}


You can use the Object array passsed into the factory to identify the matching constructor by making an array of class parameters from it.

EG:

Object myClass = Class.forName(operatorClass);
Class[] ca = new Class[o.length];

for (int i = 0; i < ca.length; i++) {
  ca[i] = Class.forName(o[i].CLASS.getName())
}

Once you have an array of Class objects you can use this it Like this...

Constructor c = myClass.getConstructor(ca);

You now have the required constructor matching an array of parameters you have passed in at run time. To create your object do...

Object ob = c.newInstance(o);

If the new instance returned is NOT an operator type or the instantiation fails you can check for this at runtime. Use the "instance of" operator to check whether it is an operator.

Thus they can create classes and instantiate them by class name through your factory - if they are not of type operator then this instantiation will fail.

Just a thought anyway.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ianBCommented:
Community Support has reduced points to 100
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.