Abstract Class Extenstions Not exactly working as I would expect.

Discussion in 'Java' started by Michael Boocher via JavaKB.com, May 26, 2005.

  1. I ran into a strange initialization order that was in my mind, counter
    initutive. What I am looking for is for anyone to perhaps offer an
    explanation to this snipet of code.

    ############### InitializeTest.java ########################
    public class InitializeTest extends TestAbstract {
    private String TheData = "Default Value -- Why THIS?";
    public static void main(String[] args){
    InitializeTest IT = new InitializeTest("Some Default Value");
    System.out.println(IT);
    }
    public InitializeTest(String InitialValue){
    super(InitialValue);
    }
    public void ForwardCall(String pData) {
    // Use to initialize Data.
    TheData = pData;
    }
    public String toString(){
    return TheData;
    }
    }

    ############### TestAbstract.java ########################
    public abstract class TestAbstract {
    public TestAbstract(String Data){
    ForwardCall(Data);
    }
    public abstract void ForwardCall(String Data);
    }


    Now, I know why it is happening I understand that the constructor order is
    a bit off. And that is why the code in the Extended class is being called
    before the class is actually instantiated. But I wonder if this is
    actually the intended result.

    This is more of a cute dicussion thread than anything else.

    Enjoy.
    --
    Michael Boocher
    Michael Boocher via JavaKB.com, May 26, 2005
    #1
    1. Advertising

  2. On 2005-05-26, Michael Boocher via JavaKB.com <> wrote:
    > I ran into a strange initialization order that was in my mind, counter
    > initutive. What I am looking for is for anyone to perhaps offer an
    > explanation to this snipet of code.


    [snip snippet]

    > Now, I know why it is happening I understand that the constructor order is
    > a bit off. And that is why the code in the Extended class is being called
    > before the class is actually instantiated. But I wonder if this is
    > actually the intended result.


    The superclass constructor is invoked before the instance variables of a
    class are initialized. The details are in the "Creation of New Class
    Instances" section of the Java Language Specification at

    http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44670
    A. Bolmarcich, May 26, 2005
    #2
    1. Advertising

  3. Michael Boocher via JavaKB.com

    John McGrath Guest

    On 5/26/2005 at 12:04:01 PM, Michael Boocher via JavaKB.com wrote:

    > I ran into a strange initialization order that was in my mind, counter
    > initutive. What I am looking for is for anyone to perhaps offer an
    > explanation to this snipet of code.


    > Now, I know why it is happening I understand that the constructor order
    > is a bit off. And that is why the code in the Extended class is being
    > called before the class is actually instantiated. But I wonder if this
    > is actually the intended result.


    Yes, it is *exactly* the intended result. The Java Language Specification
    spells out the process in detail:

    http://tinyurl.com/bsdy3
    A.K.A:
    http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#4
    4670

    <nitpick>

    Note that the superclass constructor is not called before the object is
    instantiated. Instantiation is what occurs *before* the constructors are
    called - allocation of memory and setting the member variables to their
    default values (e.g. null, 0, false).

    </nitpick>

    --
    Regards,

    John McGrath
    John McGrath, May 26, 2005
    #3
  4. Michael Boocher via JavaKB.com, May 26, 2005
    #4
  5. Michael Boocher via JavaKB.com

    Tim Ward Guest

    "Michael Boocher via JavaKB.com" <> wrote in message
    news:...
    >
    > Yeah I figured that bit out. Scary that you know the docs that well...


    This is a very well known design error to anyone who's played with Java for
    a while ...

    OK, so you'll be told "there's nothing wrong with the language design, just
    don't call virtual methods in base class constructors". Yeah, right. Like
    you get the choice - nobody seems to have told the authors of Swing this
    rule, for example.

    --
    Tim Ward
    Brett Ward Limited - www.brettward.co.uk
    Tim Ward, May 27, 2005
    #5
  6. Documentation or not, it just doesn't seem inituitive. To me at least.
    --
    Michael Boocher


    Message posted via http://www.javakb.com
    Michael Boocher via JavaKB.com, May 29, 2005
    #6
  7. Michael Boocher via JavaKB.com

    John McGrath Guest

    On 5/28/2005 at 7:15:03 PM, Michael Boocher via JavaKB.com wrote:

    > Documentation or not, it just doesn't seem inituitive. To me at least.


    What initialization order do you think would be intuitive?

    --
    Regards,

    John McGrath
    John McGrath, May 30, 2005
    #7
  8. I would have expected.... that java created the Super class then the
    derived. but Magically have called the Initializers of the derived class
    before the constructor of the super class Calls Forward.

    Its ugly. I know, but thats what I would have expected.
    --
    Michael Boocher


    Message posted via http://www.javakb.com
    Michael Boocher via JavaKB.com, May 30, 2005
    #8
  9. Michael Boocher via JavaKB.com

    John McGrath Guest

    On 5/30/2005 at 2:11:03 AM, Michael Boocher via JavaKB.com wrote:

    > I would have expected.... that java created the Super class then the
    > derived. but Magically have called the Initializers of the derived class
    > before the constructor of the super class Calls Forward.
    >
    > Its ugly. I know, but thats what I would have expected.


    If you do that, you can have the subclass using methods and fields of the
    superclass before it has been initialized properly. Take the following
    code:

    class Base {
    Dimension size;
    public Base() {
    size = new Dimension( 10, 20 );
    }
    public int getWidth() {
    return size.width;
    }
    }

    Assuming that initialization order were used with the above example, if
    getWidth() were called from a subclass initializer, you would get an NPE,
    since the Base() constructor would not have been called yet.

    --
    Regards,

    John McGrath
    John McGrath, May 30, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Kaeppler
    Replies:
    1
    Views:
    437
    R.F. Pels
    May 22, 2005
  2. Replies:
    4
    Views:
    813
    Rolf Magnus
    May 17, 2006
  3. whit
    Replies:
    1
    Views:
    604
    James Kanze
    Mar 19, 2009
  4. Simon Strandgaard

    how to expect eof with expect+pty

    Simon Strandgaard, Dec 20, 2006, in forum: Ruby
    Replies:
    4
    Views:
    352
    Simon Strandgaard
    Dec 20, 2006
  5. Guest
    Replies:
    8
    Views:
    148
Loading...

Share This Page