Derived class & objects to parent constructor

D

Doug

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
 
A

Adam

Doug said:
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
 
G

Guest

Doug said:
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;
}
}
 
D

Doug

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
 
N

Neal Gafter

Doug said:
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;
}
}
 
J

John C. Bollinger

Doug said:
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
(e-mail address removed)
 
D

Doug

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
 
X

xarax

John C. Bollinger said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top