passing derived instance variables to base

P

Paul J. Lucas

Given:

class Base {
protected Base( int[] a ) {
m_a = a;
}
final int[] m_a;
}

class Derived extends Base {
public Derived() {
super( x ); // illegal
}

private int[] x = new int[1];
}

generates:

cannot reference x before supertype constructor has been called

I need to be able to pass a derive-class-constructed array to a
base class. This is certainly possible in C++. Among other
solutions:

class Base {
protected:
Base( int* a ) {
m_a = a;
}
int* m_a;
};

class Derived : public Base {
public:
Derived() : Base( x = new int[1] ) { }
private:
int* x;
};

This doesn't appear to be possible in Java due to an
unnecessary and annoying restriction. :-(

Is it possible? If so, how?

- Paul
 
A

Adam

Paul J. Lucas said:
Given:

class Base {
protected Base( int[] a ) {
m_a = a;
}
final int[] m_a;
}

class Derived extends Base {
public Derived() {
super( x ); // illegal
}

private int[] x = new int[1];
}

generates:

cannot reference x before supertype constructor has been called

I need to be able to pass a derive-class-constructed array to a
base class.

How about another constructor:
public Derived(int[] x) { super(x); } ?

Or do it that way:

class Base
{
private int[] m_a;
Base(){ m_a = createMember(); }
protected int[] createMember() { return ... }
....
}

class Derived extends Base
{
protected int[] createMember() { return something else }
}

Java uses polimorphic calls in base constructors (unlike C++),
so when you create Derived object, then in Base construcor
Derived.createMember will be actually called.

Adam
 
J

Jos A. Horsmeier

Adam said:
Paul J. Lucas said:
Given:

class Base {
protected Base( int[] a ) {
m_a = a;
}
final int[] m_a;
}

class Derived extends Base {
public Derived() {
super( x ); // illegal
}

private int[] x = new int[1];
}

generates:

cannot reference x before supertype constructor has been called

I need to be able to pass a derive-class-constructed array to a
base class.

How about another constructor:
public Derived(int[] x) { super(x); } ?

Or do it that way:

class Base
{
private int[] m_a;
Base(){ m_a = createMember(); }
protected int[] createMember() { return ... }
...
}

class Derived extends Base
{
protected int[] createMember() { return something else }
}

Java uses polimorphic calls in base constructors (unlike C++),
so when you create Derived object, then in Base construcor
Derived.createMember will be actually called.

That certainly would work, but I *think* (see below for a disclaimer)
that the following should work also --

class Base {
protected Base( int[] a ) { m_a = a; }
final int[] m_a;
}

class Derived extends Base {
public Derived() { super(new int[1]); }
private int[] x = m_a;
}

Objects are initialized in this order --

1) member initializers
2) constructors

Super classes are initialized before derived classes are initialized,
so m_a should have been initialized before the 'int[] x= m_a' initializer
gets its turn.

kind regards,

Jos

disclaimer -- this is from the top of my head; no JLS available nor
any way to test this at this moment, so be gentle with me.
 
A

Adam

class Base {
protected Base( int[] a ) {
m_a = a;
}
final int[] m_a;
}

class Derived extends Base {
public Derived() {
super( x ); // illegal
}

private int[] x = new int[1];
}
[cut]
That certainly would work, but I *think* (see below for a disclaimer)
that the following should work also --

class Base {
protected Base( int[] a ) { m_a = a; }
final int[] m_a;
}

class Derived extends Base {
public Derived() { super(new int[1]); }
private int[] x = m_a;
}

Objects are initialized in this order --

1) member initializers
2) constructors

Super classes are initialized before derived classes are initialized,
so m_a should have been initialized before the 'int[] x= m_a' initializer
gets its turn.

kind regards,

Jos

I guess it probably will work, but i don't think that is what
the author wanted to achieve. As I understand
it is wanted to have m_a set from Derived while
the Base class needs it in the constructor for setting it.
(am I making any sense?)
So in the author's example int[] x is used as a value
to be passed to Base constructor,
nothing else.
It's not necessary to have it in Derived
(as in your example int[] x = m_a ),
at least that's what I suppose.
So the objective here would be the size of
array, right here:
Derived() { super(new int[1]; }
If this 1 or any other number can be hardcoded,
than it's sufficient to put it right there.

regards,
Adam
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top