Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Overwrite Default Constructor ?

Reply
Thread Tools

Overwrite Default Constructor ?

 
 
Philipp Leitner
Guest
Posts: n/a
 
      04-17-2006
hi all,

this may be a silly question, but is there actually any way to
_overwrite_ a constructor of a superclass in Java?

I have the following inheritance structure:

"ProfileAdmin" inherits from "ProfileSearcher"

both classes are almost identical, the main difference is that the
constructor of ProfileSearcher throws an exception in certain
circumstances, while ProfileAdmin should try to repair the problem in
these cases (with the reason that ProfileSearcher should be used on thin
clients, which are not capable of the cpu-heavy repair actions).

My first idea would have been to just overwrite the constructor of
ProfileSearcher, but I quickly discovered that javac inserts an implicit
"super()" at the beginning of the subclasses' constructor (I really had
no clue about that in advance ...)

Any tips on how to solve this issue in a clean way?

thx in advance,
Philipp

--
Philipp Wolfgang Leitner, Bakk.rer.soc.oec.
0225511
Vienna University of Technology

"Ask five economists and you'll get five different explanations (six if
one went to Harvard).
-- Edgar R. Fiedler"
 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      04-17-2006
Oliver Wong wrote:
>
> "Philipp Leitner" <(E-Mail Removed)> wrote in message
> news:4443cbe1$0$11352$(E-Mail Removed) ...
>>
>> "ProfileAdmin" inherits from "ProfileSearcher"
>>
>> both classes are almost identical, the main difference is that the
>> constructor of ProfileSearcher throws an exception in certain
>> circumstances, while ProfileAdmin should try to repair the problem in
>> these cases (with the reason that ProfileSearcher should be used on
>> thin clients, which are not capable of the cpu-heavy repair actions).

>
> You could have ProfileAdmin catch the exception. But are you sure
> ProfileAdmin should be a subclass of ProfileSearcher?


Tricky to catch an exception thrown from a super constructor call...

> Perhaps you can add a protected constructor in ProfileSearcher which
> "does nothing" (or at least, doesn't throw exceptions), which your
> subclasses (e.g. ProfileAdmin) could use.


Yup. I'd go further to suggest making non-leaf classes abstract.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      04-17-2006

"Philipp Leitner" <(E-Mail Removed)> wrote in message
news:4443cbe1$0$11352$(E-Mail Removed) ...
> hi all,
>
> this may be a silly question, but is there actually any way to _overwrite_
> a constructor of a superclass in Java?


Not to my knowledge.

>
> I have the following inheritance structure:
>
> "ProfileAdmin" inherits from "ProfileSearcher"
>
> both classes are almost identical, the main difference is that the
> constructor of ProfileSearcher throws an exception in certain
> circumstances, while ProfileAdmin should try to repair the problem in
> these cases (with the reason that ProfileSearcher should be used on thin
> clients, which are not capable of the cpu-heavy repair actions).


You could have ProfileAdmin catch the exception. But are you sure
ProfileAdmin should be a subclass of ProfileSearcher?

>
> My first idea would have been to just overwrite the constructor of
> ProfileSearcher, but I quickly discovered that javac inserts an implicit
> "super()" at the beginning of the subclasses' constructor (I really had no
> clue about that in advance ...)
>
> Any tips on how to solve this issue in a clean way?


Perhaps you can add a protected constructor in ProfileSearcher which
"does nothing" (or at least, doesn't throw exceptions), which your
subclasses (e.g. ProfileAdmin) could use.

- Oliver

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      04-17-2006
"Thomas Hawtin" <(E-Mail Removed)> wrote in message
news:4443da87$0$9274$(E-Mail Removed)...
> Oliver Wong wrote:
>>
>> "Philipp Leitner" <(E-Mail Removed)> wrote in message
>> news:4443cbe1$0$11352$(E-Mail Removed) ...
>>>
>>> "ProfileAdmin" inherits from "ProfileSearcher"
>>>
>>> both classes are almost identical, the main difference is that the
>>> constructor of ProfileSearcher throws an exception in certain
>>> circumstances, while ProfileAdmin should try to repair the problem in
>>> these cases (with the reason that ProfileSearcher should be used on thin
>>> clients, which are not capable of the cpu-heavy repair actions).

>>
>> You could have ProfileAdmin catch the exception. But are you sure
>> ProfileAdmin should be a subclass of ProfileSearcher?

>
> Tricky to catch an exception thrown from a super constructor call...


I was aware of the rule that "super()" has to be the first statement,
but I wasn't aware that a "try-catch" block "counts" as a statement. So I
tried the obvious:

<code>
public class Foo {
public Foo() {
try {
super();
} catch (Throwable t) {

}
}
}
</code>

and indeed it doesn't compile.

- Oliver

 
Reply With Quote
 
Philipp Leitner
Guest
Posts: n/a
 
      04-17-2006
well, I tried the same before myself ... and yes, this doesn't compile ...

for now I went with the idea with a "dummy" protected constructor, but I
have to say I don't like the solution much. Perhaps I am really going to
change the inheritance into an aggregation of some kind.

/philipp

Oliver Wong schrieb:
> "Thomas Hawtin" <(E-Mail Removed)> wrote in message
> news:4443da87$0$9274$(E-Mail Removed)...
>> Oliver Wong wrote:
>>>
>>> "Philipp Leitner" <(E-Mail Removed)> wrote in message
>>> news:4443cbe1$0$11352$(E-Mail Removed) ...
>>>>
>>>> "ProfileAdmin" inherits from "ProfileSearcher"
>>>>
>>>> both classes are almost identical, the main difference is that the
>>>> constructor of ProfileSearcher throws an exception in certain
>>>> circumstances, while ProfileAdmin should try to repair the problem
>>>> in these cases (with the reason that ProfileSearcher should be used
>>>> on thin clients, which are not capable of the cpu-heavy repair
>>>> actions).
>>>
>>> You could have ProfileAdmin catch the exception. But are you sure
>>> ProfileAdmin should be a subclass of ProfileSearcher?

>>
>> Tricky to catch an exception thrown from a super constructor call...

>
> I was aware of the rule that "super()" has to be the first statement,
> but I wasn't aware that a "try-catch" block "counts" as a statement. So
> I tried the obvious:
>
> <code>
> public class Foo {
> public Foo() {
> try {
> super();
> } catch (Throwable t) {
>
> }
> }
> }
> </code>
>
> and indeed it doesn't compile.
>
> - Oliver

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-17-2006
On Mon, 17 Apr 2006 19:42:02 GMT, "Oliver Wong" <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>but I wasn't aware that a "try-catch" block "counts" as a statement. So I
>tried the obvious:


To avoid the violating the super comes first rule, you need something
like this:

super( doeseverythingbuteat( parm1 ) );

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Philipp Leitner
Guest
Posts: n/a
 
      04-17-2006


> To avoid the violating the super comes first rule, you need something
> like this:
>
> super( doeseverythingbuteat( parm1 ) );
>


sorry, I don't get that How would something like that help me catch
the exception that super() throws?

/philipp
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-17-2006
Philipp Leitner wrote:
>
>
>> To avoid the violating the super comes first rule, you need something
>> like this:
>>
>> super( doeseverythingbuteat( parm1 ) );
>>

>
> sorry, I don't get that How would something like that help me catch
> the exception that super() throws?
>
> /philipp


It doesn't, but it may help you prevent the exception. In some cases you
could check for the condition that causes the exception, and prevent it
from happening. It is most useful for things like incorrect parameters.

Patricia
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-17-2006
On Mon, 17 Apr 2006 22:34:31 +0200, Philipp Leitner
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>> super( doeseverythingbuteat( parm1 ) );
>>

>
>sorry, I don't get that How would something like that help me catch
>the exception that super() throws?


It wouldn't, but you could do a test to detect the condition that
would upset super and handle it.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Philipp Leitner
Guest
Posts: n/a
 
      04-17-2006

> It doesn't, but it may help you prevent the exception. In some cases you
> could check for the condition that causes the exception, and prevent it
> from happening. It is most useful for things like incorrect parameters.


ok, now I get what you meant; unfortunately this would be quite strange
(if not impossible) to do in my case since I heavily depend on member
variables.

/philipp
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
constructor overwrite Mug Python 3 02-15-2010 03:05 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
stringstream overwrite with constructor vs. << martinezfive@comcast.net C++ 9 01-05-2007 07:07 PM
How to call default constructor from other constructor of the same class? romayankin@gmail.com C++ 9 03-06-2006 09:30 PM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments