Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Derived class & objects to parent constructor

Reply
Thread Tools

Derived class & objects to parent constructor

 
 
Doug
Guest
Posts: n/a
 
      05-12-2004
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
 
Reply With Quote
 
 
 
 
Adam
Guest
Posts: n/a
 
      05-12-2004

"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


 
Reply With Quote
 
 
 
 
=?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRoZSBv76yDY2XigKYpIg==?=
Guest
Posts: n/a
 
      05-12-2004
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;
}
}
 
Reply With Quote
 
Doug
Guest
Posts: n/a
 
      05-12-2004
"Adam" <(E-Mail Removed)> wrote in news:c7tasc$24n
$(E-Mail Removed):

> 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
 
Reply With Quote
 
Neal Gafter
Guest
Posts: n/a
 
      05-12-2004
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;
}
}

 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      05-12-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Doug
Guest
Posts: n/a
 
      05-12-2004
Neal Gafter <(E-Mail Removed)> wrote in
news:lproc.10935$(E-Mail Removed):

> 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
 
Reply With Quote
 
xarax
Guest
Posts: n/a
 
      05-13-2004
"John C. Bollinger" <(E-Mail Removed)> wrote in message
news:c7tiju$vuj$(E-Mail Removed)...
> 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.


 
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
call base class constructor from derived class constructor Rahul C++ 16 11-07-2007 03:40 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
Invoking templatized base class constructor from templatized derived class constructor mrstephengross C++ 5 05-18-2005 07:12 PM



Advertisments