Hi,
I wish to override a method of a class A (I cannot alter class A)
I plan to do this by creating a class B which extends A, and overrides
the method.
When implementing class B, I cannot compile unless I create a
constructor in class B which calls super(<params>)
The problem is that super(<params>) calls the method I wish to
override, and with the given parameters throws an exception...
Is there someway to extend a class without calling the constructor of
the class being extended?
Regards, Per Magnus
VisionSet - 22 Nov 2005 14:31 GMT
> Hi,
> I wish to override a method of a class A (I cannot alter class A)
[quoted text clipped - 4 lines]
> The problem is that super(<params>) calls the method I wish to
> override, and with the given parameters throws an exception...
The method you write in class B will be exceuted for the instance of class B
even if it is called from Class A's constructor
> Is there someway to extend a class without calling the constructor of
> the class being extended?
No
--
Mike W
J. Verdrengh - 22 Nov 2005 14:36 GMT
> Is there someway to extend a class without calling the constructor of
> the class being extended?
No, a superconstructors has always to be called by a (sub)constructor. If
you don't explicitly define a constructor, then the superconstructor with no
parameters is implicitly called (*)
> The problem is that super(<params>) calls the method I wish to
> override, and with the given parameters throws an exception...
I don't see the problem, please explain more in detail
(*) Only if the superclass has no explicitly defined constructors with more
then 0 params, otherwise the compiler will give an error
Roedy Green - 22 Nov 2005 14:43 GMT
>Is there someway to extend a class without calling the constructor of
>the class being extended?
Any B constructor is also constructing/initialialising an inner A
object. Therefore it must first call SOME constructor of A.
If you don't do that, the compiler will often insert code for you that
does.
Constructors should avoid methods that are overridden. It creates
nightmarish can of worms and all kinds of very hard to figure out
bugs. I suspect, had it been easy to enforce, the JLS would have
banned the use of non final methods in constructors.
http://mindprod.com/jgloss/constructor.html
and follow links.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Chris Uppal - 22 Nov 2005 14:46 GMT
> Is there someway to extend a class without calling the constructor of
> the class being extended?
No.
The nearest you can come is something like the following. If you have:
class A
{
A(int i)
{
theMethod(int i);
}
void theMethod(int i) { }
}
And you want to create a subclass B which overrides theMethod() but without it
being called during initialisation. So:
class B
{
boolean intialized;
B(int i)
{
super(i);
initialized = true;
}
void theMethod(int i)
{
if (!initialized)
{
super.theMethod(i);
return;
}
// ...other code...
}
}
Note that doing this sort of thing is normally a symptom of a design error
somewhere (possibly in the class you wish to subclass from, possibly in what
you are trying to do with it).
-- chris
Ravi - 26 Nov 2005 07:28 GMT
Hi,
Even in the above case also when we call super(i) the overriden method
is called from construtcor A...hence the problem is not solved here....
Bye,
Ravi.
Chris Uppal - 26 Nov 2005 11:19 GMT
> Even in the above case also when we call super(i) the overriden method
> is called from construtcor A...hence the problem is not solved here....
I didn't say the method wasn't called, I said that the nearest you could get to
the OP's idea was [...etc...].
-- chris
Vitaly - 27 Nov 2005 10:53 GMT
You can create Class Delegator in runtime. See the idea in BCEL Project.
> Hi,
> Even in the above case also when we call super(i) the overriden method
> is called from construtcor A...hence the problem is not solved here....
>
> Bye,
> Ravi.
Thomas Hawtin - 22 Nov 2005 14:49 GMT
> I wish to override a method of a class A (I cannot alter class A)
> I plan to do this by creating a class B which extends A, and overrides
[quoted text clipped - 3 lines]
> The problem is that super(<params>) calls the method I wish to
> override, and with the given parameters throws an exception...
Constructors calling (virtual) methods are bad (though not as bad as in
C++). Best avoided.
If you really, really have to then make the overriding method correct
even before you constructor has executed. There are hacks with
thread-locals, synchronised statics or inner classes (with -target 1.4
or greater) to hand context information over.
> Is there someway to extend a class without calling the constructor of
> the class being extended?
You could rewrite the bytecode of A. Possibly if it is Serializable, you
could hack around the constructor calling the method, but the method
would probably get called during deserialisation anyway.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Thomas Kellerer - 27 Nov 2005 11:21 GMT
kebabkongen@hotmail.com wrote on 22.11.2005 15:24:
> Hi,
> I wish to override a method of a class A (I cannot alter class A)
[quoted text clipped - 9 lines]
>
> Regards, Per Magnus
This should work:
ClassA classAVar = new ClassA(<params>)
{
public void fixMeMethod()
{
// do the correct work here
}
};
Regards
Thomas