Derived class & objects to parent constructor

Discussion in 'Java' started by Doug, May 12, 2004.

  1. Doug

    Doug Guest

    I've worked around this problem but it's still bugging me a bit so I
    thought I'd ask here. In a derived class, I wanted to hold a reference to
    a newly created object passed to a parent constructor. The only public
    constructor in the parent requires this argument and the parent provides no
    methods to later retrieve it. I couldn't find a syntax that would compile.
    I'm thinking it's not possible but just wanted to confirm that I wasn't
    missing something.

    class someobject {}

    class parent {
    public parent (someobject so) { }
    }

    class child extends parent {
    // someobject so = new someobject(); // Doesn't work
    public child () {
    // someobject so = new someobject(); // Also doesn't work
    super (new someobject ()); // Want to hold a reference
    // to someobject in child
    }
    }

    Thanks in advance for any answers.

    Doug
    Doug, May 12, 2004
    #1
    1. Advertising

  2. Doug

    Adam Guest

    "Doug" <nothanks@127.0.0.1> wrote in message
    news:Xns94E76529E8ADBnoemail127001@207.69.154.202...
    > I've worked around this problem but it's still bugging me a bit so I
    > thought I'd ask here. In a derived class, I wanted to hold a

    reference to
    > a newly created object passed to a parent constructor. The only

    public
    > constructor in the parent requires this argument and the parent

    provides no
    > methods to later retrieve it. I couldn't find a syntax that would

    compile.
    > I'm thinking it's not possible but just wanted to confirm that I

    wasn't
    > missing something.
    >
    > class someobject {}
    >
    > class parent {
    > public parent (someobject so) { }
    > }
    >
    > class child extends parent {
    > // someobject so = new someobject(); // Doesn't work
    > public child () {
    > // someobject so = new someobject(); // Also doesn't work
    > super (new someobject ()); // Want to hold a reference
    > // to someobject in child
    > }
    > }


    class Child extends Parent
    {
    SomeObject obj;
    Child()
    {
    super(obj = new SomeObject());
    }
    }

    or

    class Child extends Parent
    {
    SomeObject obj;
    Child(SomeObject param)
    {
    super(param);
    obj = param;
    }

    static Child createInstance() { return new Child(new
    SomeObject()); }
    }

    hth,
    Adam
    Adam, May 12, 2004
    #2
    1. Advertising

  3. Doug wrote:
    > I've worked around this problem but it's still bugging me a bit so I
    > thought I'd ask here. In a derived class, I wanted to hold a reference to
    > a newly created object passed to a parent constructor. The only public
    > constructor in the parent requires this argument and the parent provides no
    > methods to later retrieve it. I couldn't find a syntax that would compile.
    > I'm thinking it's not possible but just wanted to confirm that I wasn't
    > missing something.
    >
    > class someobject {}
    >
    > class parent {
    > public parent (someobject so) { }
    > }
    >
    > class child extends parent {
    > // someobject so = new someobject(); // Doesn't work
    > public child () {
    > // someobject so = new someobject(); // Also doesn't work
    > super (new someobject ()); // Want to hold a reference
    > // to someobject in child
    > }
    > }


    class child extends parent {
    final someobject so;
    public child (someobject so) {
    super (so);
    this.so = so;
    }
    }
    =?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRo, May 12, 2004
    #3
  4. Doug

    Doug Guest

    "Adam" <> wrote in news:c7tasc$24n
    $:

    > class Child extends Parent
    > {
    > SomeObject obj;
    > Child()
    > {
    > super(obj = new SomeObject());
    > }
    > }


    Forgot to mention I tried this one as well. It results in an error:

    cannot reference obj before supertype constructor has been called

    Thanks,
    Doug
    Doug, May 12, 2004
    #4
  5. Doug

    Neal Gafter Guest

    Doug wrote:
    > I've worked around this problem but it's still bugging me a bit so I
    > thought I'd ask here. In a derived class, I wanted to hold a reference to
    > a newly created object passed to a parent constructor. The only public
    > constructor in the parent requires this argument and the parent provides no
    > methods to later retrieve it. I couldn't find a syntax that would compile.
    > I'm thinking it's not possible but just wanted to confirm that I wasn't
    > missing something.
    >
    > class someobject {}
    >
    > class parent {
    > public parent (someobject so) { }
    > }
    >
    > class child extends parent {
    > // someobject so = new someobject(); // Doesn't work
    > public child () {
    > // someobject so = new someobject(); // Also doesn't work
    > super (new someobject ()); // Want to hold a reference
    > // to someobject in child
    > }
    > }


    It is possible but a tiny bit awkward. The trick is to use constructor chaining
    to interpose a local variable:

    class Someobject {}

    class Parent {
    public Parent(Someobject so) {}
    }

    class Child extends Parent {
    final Someobject so;
    public Child() {
    this(new Someobject());
    }
    private Child(Someobject so) {
    super(so);
    this.so = so;
    }
    }
    Neal Gafter, May 12, 2004
    #5
  6. Doug wrote:

    > I've worked around this problem but it's still bugging me a bit so I
    > thought I'd ask here. In a derived class, I wanted to hold a reference to
    > a newly created object passed to a parent constructor. The only public
    > constructor in the parent requires this argument and the parent provides no
    > methods to later retrieve it. I couldn't find a syntax that would compile.
    > I'm thinking it's not possible but just wanted to confirm that I wasn't
    > missing something.
    >
    > class someobject {}
    >
    > class parent {
    > public parent (someobject so) { }
    > }
    >
    > class child extends parent {
    > // someobject so = new someobject(); // Doesn't work
    > public child () {
    > // someobject so = new someobject(); // Also doesn't work
    > super (new someobject ()); // Want to hold a reference
    > // to someobject in child
    > }
    > }


    The best and easiest choice is to give the subclass' constructor a
    matching parameter that it can pass through to the superclass
    constructor, and can also store in the subclass' data. That requires
    the object argument to be constructed externally.

    An alternative, if you _must_ do this, might be:

    class child extends parent {

    someobject so;

    public child() {
    super(so = new someObject());
    }
    }

    I haven't tested that, and it's poor form, but it might work.

    If it's okay for every child to use (and initialize its parent with) the
    same someobject, then you could create a static instance and use that.


    John Bollinger
    John C. Bollinger, May 12, 2004
    #6
  7. Doug

    Doug Guest

    Neal Gafter <> wrote in
    news:lproc.10935$:

    > It is possible but a tiny bit awkward. The trick is to use
    > constructor chaining to interpose a local variable:
    >


    Thank you very much, this is what I was seeking. Turns out to be one of
    those "why didn't I think of that?" things.

    Doug
    Doug, May 12, 2004
    #7
  8. Doug

    xarax Guest

    "John C. Bollinger" <> wrote in message
    news:c7tiju$vuj$...
    > Doug wrote:
    >
    > > I've worked around this problem but it's still bugging me a bit so I
    > > thought I'd ask here. In a derived class, I wanted to hold a reference to
    > > a newly created object passed to a parent constructor. The only public
    > > constructor in the parent requires this argument and the parent provides no
    > > methods to later retrieve it. I couldn't find a syntax that would compile.
    > > I'm thinking it's not possible but just wanted to confirm that I wasn't
    > > missing something.
    > >
    > > class someobject {}
    > >
    > > class parent {
    > > public parent (someobject so) { }
    > > }
    > >
    > > class child extends parent {
    > > // someobject so = new someobject(); // Doesn't work
    > > public child () {
    > > // someobject so = new someobject(); // Also doesn't work
    > > super (new someobject ()); // Want to hold a reference
    > > // to someobject in child
    > > }
    > > }

    >
    > The best and easiest choice is to give the subclass' constructor a
    > matching parameter that it can pass through to the superclass
    > constructor, and can also store in the subclass' data. That requires
    > the object argument to be constructed externally.
    >
    > An alternative, if you _must_ do this, might be:
    >
    > class child extends parent {
    >
    > someobject so;
    >
    > public child() {
    > super(so = new someObject());
    > }
    > }
    >
    > I haven't tested that, and it's poor form, but it might work.


    It won't work. You cannot have any reference, either implicit
    or explicit, to "this" in a constructor before the super
    constructor has returned. The reference to "so = new ..."
    is illegal.

    Posted elsewhere was the "correct" solution to use
    an alternative constructor.

    public class Child extends Parent
    {
    SomeObject so;
    public Child(SomeObject theSo)
    {
    super(theSo);
    so = theSo;
    }

    public Child()
    {
    this(new SomeObject());
    }
    }

    btw: the constructor reference this(...) is not a
    reference to the currently constructed instance,
    even though it uses the "this" keyword. Constructors
    are class methods, not instance methods.
    xarax, May 13, 2004
    #8
    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. mrstephengross
    Replies:
    5
    Views:
    584
    Larry I Smith
    May 18, 2005
  2. ali
    Replies:
    4
    Views:
    558
    David Harmon
    Mar 5, 2007
  3. Replies:
    1
    Views:
    384
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    372
    Victor Bazarov
    May 23, 2007
  5. Rahul
    Replies:
    16
    Views:
    1,176
    terminator
    Nov 7, 2007
Loading...

Share This Page