Extend a class without calling the constructor

K

kebabkongen

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
 
V

VisionSet

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...

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
 
J

J. Verdrengh

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
 
R

Roedy Green

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.
 
C

Chris Uppal

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
 
T

Thomas Hawtin

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
 
R

Ravi

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.
 
C

Chris Uppal

Ravi said:
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
 
T

Thomas Kellerer

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

This should work:

ClassA classAVar = new ClassA(<params>)
{
public void fixMeMethod()
{
// do the correct work here
}
};

Regards
Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top