Article View: borland.public.jbuilder.java.api
Article #1605Re: Is this a bug ???
From: John Baker
Date: Mon, 05 Nov 2001 09:40
Date: Mon, 05 Nov 2001 09:40
107 lines
3772 bytes
3772 bytes
Interesting example but I think java is still is working the specified way
as the super class must be completely contructed before constructing the
subclass so your field will get overwritten every time.
I wouldn't really recommend this kind of initialization strategy and I
suspect you are going for some kind custom module type scenario.
More often that not it is better to use an interface as the basis of some
module and have an adapter class (default implementation for simplicity like
MouseAdapter etc) that you extend from. This leaves you the option later to
not extend from the base class if necessary (this often happens when a
program evolves and requirements change). An example if you like..
// The basic skeletal definition of a module
public interface MyModule
{
public void init();
public void open();
public void close();
public void destroy();
}
// A convenient adapter class to extend to avoid rewriting every method
public class MyModuleDefaultImpl implements MyModule
{
public void init() { /* noop - override if req */ }
public void open() { /* noop - override if req */ }
public void close() { /* noop - override if req */ }
public void destroy() { /* noop - override if req */ }
}
// This
public class MyModuleImplA extends MyModuleDefaultImpl
{
public void init() { /* override to do something useful */ }
}
public class HostApp
{
public static void main(String[] args)
{
try
{
// Use a dynamically loaded and instantiated module at runtime
// Notice we only ever refer to the interface not the actual
implementation at runtime
MyModule m = (MyModule)Class.forName("MyModuleImplA").newInstance();
m.init();
m.open(); // etc....
}
catch (Exception x)
{
x.printStackTrace();
}
}
}
Hanan Hermon <hananh@radcom.co.il> wrote in message news:3be65516_2@dnews...
> Hi,
>
> Below there are to small classes with a main method.
> When I run it I expected the output to be "Init", but surprisingly it was
> "Def".
>
> I found out that when we set default values in the class descriptions it
> is being set after we return from the ancestor constructor.
>
> I expected that the default values will be set before the constructor
> starts.
>
> Is this a bug or that is the way it should work ?
>
> I'm using JBuilder 4 on Windows-NT.
>
> Here ate the classes descriptions:
>
> abstract class Base
> {
> abstract public void init();
>
> Base()
> {
> init();
> }
> }
>
>
> public class Sub extends Base
> {
> private String mTemp = "Def";
>
> public void init()
> {
> mTemp = "Init";
> }
>
> static public void main(String[] args)
> {
> Sub tmp = new Sub();
>
> System.out.println(tmp.mTemp); // Expected output is "Init" ?!
>
> }
> }
>
>
Technical Details
Message-ID:
<3be65bc0_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!isdnet!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnewsReferences:
<3be65516_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,25fa17041a8095b7 X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2001-11-05 01:34:22 PST From: "John Baker" <jbaker@SoftComms.com> Newsgroups: borland.public.jbuilder.java.api References: <3be65516_2@dnews> Subject: Re: Is this a bug ??? Date: Mon, 5 Nov 2001 09:40:39 -0000 Lines: 109 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2314.1300 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 NNTP-Posting-Host: 217.135.248.73 Message-ID: <3be65bc0_1@dnews> X-Trace: dnews 1004952512 217.135.248.73 (5 Nov 2001 01:28:32 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!isdnet!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews