tags:

views:

101

answers:

3

Heres the situation. Component B extends component A and overrides the init method to accept a different parameter. A also has a create method that calls init.

If i have an instance of B and i call create, its calling the wrong init - it calls init in B, where i need it to call init in A.

I dont want to call super.init() as there may not always be a super. Is there any way to specify to call the init in the parent component?

+1  A: 

call the base initializer I don't know what language you're running but in C# it's for example B b = new B() : base(parameters...) of course you'd still have to check wether the component is in fact of type A or B but you can do that in most languages.

This will also run the extened initializer, but thats the way it is. If you want to get rid of the extended objects initializer you'll just have to create a parent type object I think. It's the only thing I can think of.

But of course it would help if you specified what language you are programming in, in order to know the options and syntax.

Jonas B
Its ColdFusion, as tagged
Yisroel
Ahh my bad there, sorry
Jonas B
+2  A: 

Refactor the actual code you want to execute into their own methods, and have the Init methods call that code, and have the create method call that code as well.

It sounds like your objects are not well designed at the moment. Try to break your methods down to smaller, more constrained units.

Edit:

I think my answer still stands.

If the Init method in the parent component does some stuff, move that stuff to a new method, say, "initDoStuff(), and have the init method call that method.

Then, have your create method call the initDoStuff() method instead of init.

ColdFusion is a dynamically typed language, and you don't need to override methods just to accept different parameters. You can do that in other ways.

CF isn't able to pick methods based on argument signatures. So, if you have a situation like this, you need to handle it in a different way.

Basically, the idea of overriding a method to change its argument types is not really valid in ColdFuion.

Component A:

<cffunction name="init" access="public" output="false">
  <cfargument name=... ...>
  <cfreturn initDoSomething(argumentCollection=arguments)>
</cffunction>

<cffunction name="initDoSomething" access="package" output="false">
  {do stuff}
  <cfreturn {whatever}>
</cffunction>

Component B:

<cffunction name="create" access="package" output="false">
  <cfset {something} = initDoSomething({whatever arguments})>
  <cfreturn {whatever}>
</cffunction>
Edward M Smith
there are a few children of A. mostly the override the init method to allow different ways to init, but then they call super.init to actuall init the object. the actual code i want to execute is the parent init method, but i'm calling it from a method in the parent object, so i cant use super.init
Yisroel
that would definitely solve it, but doesnt that defeat the purpose? i could just add all the different init methods to the parent object with different names. thats what i'm trying to avoid.
Yisroel
A: 

I solved this by extending the create method to call super.create and super.init. theres still a call to the child init that fails, but overall it works.

I would still prefer a solution that works on the parent init without having to override the create.

Yisroel