Calling an overridden parent method from a parent method

This is the situation. Component B extends component A and overrides the method init

to accept another parameter. It also has a method create

that calls init

.

If I have an instance of B and I call create

it, the call is wrong init

- it calls init in B, where I need to call init in A.

I do not want to call super.init()

, because it cannot always be super

. Is there a way to specify to call init on the parent component?

+2


a source to share


3 answers


I solved this by extending the method create

to call super.create

and super.init

. theres still calls the child init which fails, but overall it works.



I would prefer a solution that works with parent init without having to override creation.

+1


a source


Refactoring the actual code you want to execute into your own methods, and the Init methods call that code, and the create method also calls that code.

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

Edit:

I think my answer is still worth it.

If the Init method in the parent component is doing something, move this stuff to a new method, say "initDoStuff (), and ask the init method to call that method."

Then create your create method with initDoStuff () method instead of init.

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



CF cannot select methods based on argument signatures. So, if you have this situation, you need to handle it differently.

Basically, the idea of ​​overriding a method to change its argument types is not 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>

      

+2


a source


call the base initializer I don't know what language you are working in, but in C # it's like B b = new B (): base (parameters ...), of course you still have to check if the component is actually of type A or B, but you can do this in most languages.

This will also cause the initializer to expand, but it does. If you want to get rid of the extended objects initializer, you just need to create an object of the parent type, I guess. This is the only thing I can think of.

But of course it will help if you have indicated what language you are programming in to find out the parameters and syntax.

+1


a source







All Articles