Re: Extend a class without calling the constructor
- From: Thomas Hawtin <usenet@xxxxxxxxxxxxxxxxx>
- Date: Tue, 22 Nov 2005 14:49:43 +0000
kebabkongen@xxxxxxxxxxx wrote:
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...
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 -- Unemployed English Java programmer http://jroller.com/page/tackline/ .
- References:
- Extend a class without calling the constructor
- From: kebabkongen
- Extend a class without calling the constructor
- Prev by Date: Re: Extend a class without calling the constructor
- Next by Date: Re: XML + XSD: Is it possible to get all possible Values for an Element?
- Previous by thread: Re: Extend a class without calling the constructor
- Next by thread: Re: Extend a class without calling the constructor
- Index(es):
Relevant Pages
|