Abstract Base Class Initialisation

G

Graeme

Hi, Would someone be able to help me here, I am missing the wood for
the trees!

I have an abstract class which I am initialising thus in the super
call:

/*Begin code snippet */
public abstract class AbstractHierarchyObject implements
HierarchyObject, Validate {
private String alias;
private String comment;
private String name;
private HierarchyObject parent;
private DataStorage dataStorage;

public AbstractHierarchyObject(String palias, String pname, String
pcomment,
HierarchyObject pparent, DataStorage pdataStorage) {
this.alias = palias;
this.comment = pcomment;
this.name = pname;
this.parent = pparent;
this.dataStorage = pdataStorage;
}
...
}
/*End code snippet */

When a valid instance of an object implementing the HierarchyObject
interface gets passed into the constructor in the pparent variable,
the this.parent line in the constructor always initialises the parent
to null.

What am I forgetting here about Abstract Classes?

Many thanks,

Graeme.
 
E

Eric Sosman

Graeme said:
Hi, Would someone be able to help me here, I am missing the wood for
the trees!

I have an abstract class which I am initialising thus in the super
call:

/*Begin code snippet */
public abstract class AbstractHierarchyObject implements
HierarchyObject, Validate {
private String alias;
private String comment;
private String name;
private HierarchyObject parent;
private DataStorage dataStorage;

public AbstractHierarchyObject(String palias, String pname, String
pcomment,
HierarchyObject pparent, DataStorage pdataStorage) {
this.alias = palias;
this.comment = pcomment;
this.name = pname;
this.parent = pparent;
this.dataStorage = pdataStorage;
}
...
}
/*End code snippet */

When a valid instance of an object implementing the HierarchyObject
interface gets passed into the constructor in the pparent variable,
the this.parent line in the constructor always initialises the parent
to null.

What am I forgetting here about Abstract Classes?

Nothing, probably. You're most likely forgetting
something having to do with the rest of the code, the part
I can't see well (my crystal ball has developed cataracts).
Perhaps if you whittled the code down to a short *complete*
example that demonstrates the problem, somebody might be
able to help.
 
R

Roedy Green

What am I forgetting here about Abstract Classes?

The problem is likely in the code that extends the abstract class
which you did not post.

You must call the abstract constructor first thing in the subclass
constructor. Check the parm names and order.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Patriotism is fierce as a fever, pitiless as the grave, blind as a stone, and as irrational as a headless hen."
~ Ambrose Bierce (born: 1842-06-24 died: 1914 at age: 71)
 
G

Graeme

You were right, I was only looking at 50% of the problem. The actual
issue lay up the object inheritance model in the line of code that
overrode the parent object, never setting it in the constructor -
school boy error!

Interfaces are much better ...

I like the http://sscce.org/ site,

Thanks for your help,

Graeme.
 

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,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top