Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird Inheritence Issue

Status
Not open for further replies.

NeilTrain

Programmer
May 27, 2003
275
US
I have a class called "Accessories" which Inherits "Surface", which inherits from the TreeNode class

A new instance of Surface class adds a class called "Region" as one of its nodes. It does this in the constructor, shown here:
Code:
public Surface()
{
	Nodes.Add(new Region());
}

The Accessories class does not add any nodes in its constructor. When a new one is created, it should have no child nodes:
Code:
public Accessories()
{
	this.Text = "Accessories";
}

Yet when my program is run, the initial accessories node has a region for a child node. When I step through the code of the Accessories constructor, the watch window tells me it already has a child node. Just to be sure, I commented out the Nodes.Add(new Region()) line of the Surface constructor, and sure enough the accessories node had no child nodes, therefore the Surface constructor is firing before the Accessories consuctor. Which is odd.

Why is this happening, and is there any way to stop it?
 
Which is odd

doesn't sound odd to me just defaul behavior, it's one of the reasons why super() has to be the first line in a constructor of a subclass. I think that if you ommit the super() that it will add one of it's own.

Normally the accesories constructor will fire the surface constructor wich will fire the treenode constructor all as the first line in their respecticve constructor so treenode will be created and then surface will go on top of that with accessories on top of that. and the last being executed will be accessories.

perhaps it would be better to create a method addnoderegion in the surface class wich you override in the accessories class with nothing in it.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Well after reading some more about it yes I guess thats exactly what its supposed to do. But what I think is odd about it that the language docs also say that constructors are NOT inherited. Well they are not inherited, but in my opinion, firing the constructor of the parent is essentially inheritence. I guess I just needed it to be cleared up.
 
What's the point of inheritance if you don't call the constructor of the superclass? But I think you already answered that for yourself.

I think they mean to say that if you have several overloaded constructors in the superclass that does won't be inherited by default by the subclass.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top