473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Inheritance Design Question

I want to provide an abstract class to derive all nodes from. In the example
below Parent in the base class is of Type Node, but in the custom class I
want Parent to return a Type CustomNode. When CustomNode is accessed by a
helper class as Node Parent is always null. Is this a poor design, Im sure
it's a common problem and I am just missing something.

Note: I use virtual for Parent because abstract will not compile.

public abstract class Node
{
public virtual Node Parent
{
get { return null; }
}
public virtual bool IsLastChild
{
get { return this._isLastChild; }
}
private bool _isLastChild = false;
}

public class CustomNode : Node
{
public new CustomNode Parent
{
get { return this._customNode; }
}
private CustomNode _customNode = null;
}
Nov 16 '05 #1
12 1481
You need to use override rather than new in the derived class.

Thomas P. Skinner [MVP]

"Mike" <mi********@com.nospam> wrote in message
news:O7****************@TK2MSFTNGP11.phx.gbl...
I want to provide an abstract class to derive all nodes from. In the
example
below Parent in the base class is of Type Node, but in the custom class I
want Parent to return a Type CustomNode. When CustomNode is accessed by a
helper class as Node Parent is always null. Is this a poor design, Im
sure
it's a common problem and I am just missing something.

Note: I use virtual for Parent because abstract will not compile.

public abstract class Node
{
public virtual Node Parent
{
get { return null; }
}
public virtual bool IsLastChild
{
get { return this._isLastChild; }
}
private bool _isLastChild = false;
}

public class CustomNode : Node
{
public new CustomNode Parent
{
get { return this._customNode; }
}
private CustomNode _customNode = null;
}

Nov 16 '05 #2
Override does not work when changing the return Type, I want to return
CustomNode which derives from Node.
Nov 16 '05 #3

"Mike" <mi********@com.nospam> wrote in message
news:e9**************@TK2MSFTNGP14.phx.gbl...
Override does not work when changing the return Type, I want to return
CustomNode which derives from Node.


You really should return Node. You are free to cast it to CustomNode if you
know it is a CustomNode.

David
Nov 16 '05 #4
Your design problem is not clear. I understand what you are trying to do,
but not why, so it is difficult to help.

It is clear that a node has a parent. It is not clear why a childnode would
need to return anything except a node type for its parent, because this
restricts the entire tree that you can describe.

What design pattern are you attempting to implement? Can you do this using
the Visitor pattern? (http://home.earthlink.net/~huston2/dp/visitor.html)

--- Nick

"Mike" <mi********@com.nospam> wrote in message
news:e9**************@TK2MSFTNGP14.phx.gbl...
Override does not work when changing the return Type, I want to return
CustomNode which derives from Node.

Nov 16 '05 #5
Since CustomNode is a Node, then any code expecting a node has a Node, but
other code written for CustomNode gets a CustomNode, and no casting is
needed. If there is a way to redesign these classes to work without casting
I am al for it as it will reduce runtime errors associated with casting.

It seems odd that the CustomNode #1 below works, because the compiler knows
that _customNode is a Node, but #2 does not, when CustomNode derives from
node. I cannot imagine any situation where #2 would cause a problem.
--What am I missing here?

#1) Works:

public class CustomNode : Node
{
public override Node Parent
{
get { return this._customNode; }
}
private CustomNode _customNode = null;
}

#2) Does Not Work:
public class CustomNode : Node
{
public override CustomNode Parent
{
get { return this._customNode; }
}
private CustomNode _customNode = null;
}
Nov 16 '05 #6
Nick,

Consider that CustomNode has:

public string Description
public string GetParentDecriptions(CustomNode node)
{
// loop node parents and return a string containing all parent
descriptions
}
"Nick Malik" <ni*******@hotmail.nospam.com> wrote in message
news:Ss2pd.141611$HA.65182@attbi_s01...
Your design problem is not clear. I understand what you are trying to do,
but not why, so it is difficult to help.

It is clear that a node has a parent. It is not clear why a childnode would need to return anything except a node type for its parent, because this
restricts the entire tree that you can describe.

What design pattern are you attempting to implement? Can you do this using the Visitor pattern? (http://home.earthlink.net/~huston2/dp/visitor.html)

--- Nick

"Mike" <mi********@com.nospam> wrote in message
news:e9**************@TK2MSFTNGP14.phx.gbl...
Override does not work when changing the return Type, I want to return
CustomNode which derives from Node.


Nov 16 '05 #7

"Mike" <mi********@com.nospam> wrote in message
news:ON**************@TK2MSFTNGP09.phx.gbl...
Since CustomNode is a Node, then any code expecting a node has a Node, but
other code written for CustomNode gets a CustomNode, and no casting is
needed. If there is a way to redesign these classes to work without
casting
I am al for it as it will reduce runtime errors associated with casting.

It seems odd that the CustomNode #1 below works, because the compiler
knows
that _customNode is a Node, but #2 does not, when CustomNode derives from
node. I cannot imagine any situation where #2 would cause a problem.
--What am I missing here?


You are correct, there's no reason why it couldn't be made to work. It just
doesn't happen to.

The feature is called "covariant return types". Java 5 and C++ and various
other OO languages support it. .NET does not.

David
Nov 16 '05 #8

"Mike" <mi********@com.nospam> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Nick,

Consider that CustomNode has:

public string Description
public string GetParentDecriptions(CustomNode node)
{
// loop node parents and return a string containing all parent
descriptions
}


For this, you can just access the private member _customNode. You aren't
limited to to the virtual accessor when implementing methods on the type.
public string GetParentDecriptions(CustomNode node)
{
StringBuilder sb = new StringBuilder();
CustomNode parent = node._customNode;
while (parent != null)
{
sb.Append(parent.Description);
parent = parent._customNode;
}
return sb.ToString();

}

David
Nov 16 '05 #9
David,

Would you say in this case, the proper solution would be to provide...

public CustomNode CustomNodeParent

....this seems like the only way to avoid casting, but it feels clunky and
maybe there is another way, or this is a poor design.
"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:uC**************@TK2MSFTNGP15.phx.gbl...

"Mike" <mi********@com.nospam> wrote in message
news:ON**************@TK2MSFTNGP09.phx.gbl...
Since CustomNode is a Node, then any code expecting a node has a Node, but other code written for CustomNode gets a CustomNode, and no casting is
needed. If there is a way to redesign these classes to work without
casting
I am al for it as it will reduce runtime errors associated with casting.

It seems odd that the CustomNode #1 below works, because the compiler
knows
that _customNode is a Node, but #2 does not, when CustomNode derives from node. I cannot imagine any situation where #2 would cause a problem.
--What am I missing here?

You are correct, there's no reason why it couldn't be made to work. It

just doesn't happen to.

The feature is called "covariant return types". Java 5 and C++ and various other OO languages support it. .NET does not.

David

Nov 16 '05 #10
Good point, but another class ( CustomNodeWriter ) that does not have
private or protected access needs to navigate a CustomNode tree Parent will
have to return a Node and I am back to casting. It seems that adding: public
CustomNode CustomParent, is the only real solution here.

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:e6**************@TK2MSFTNGP14.phx.gbl...

"Mike" <mi********@com.nospam> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Nick,

Consider that CustomNode has:

public string Description
public string GetParentDecriptions(CustomNode node)
{
// loop node parents and return a string containing all parent
descriptions
}


For this, you can just access the private member _customNode. You aren't
limited to to the virtual accessor when implementing methods on the type.
public string GetParentDecriptions(CustomNode node)
{
StringBuilder sb = new StringBuilder();
CustomNode parent = node._customNode;
while (parent != null)
{
sb.Append(parent.Description);
parent = parent._customNode;
}
return sb.ToString();

}

David

Nov 16 '05 #11

"Mike" <mi********@com.nospam> wrote in message
news:uB**************@TK2MSFTNGP12.phx.gbl...
David,

Would you say in this case, the proper solution would be to provide...

public CustomNode CustomNodeParent

...this seems like the only way to avoid casting, but it feels clunky and
maybe there is another way, or this is a poor design.


If CustomNode is an important subclass, that's what I would do.

The other solution is to switch node to an interface and use explicit
interface implementation for the parent method. Then you are free to
implement subtype-returning Parent methods on each subclass. I'm not sure
that's a better solution, though.
David
Nov 16 '05 #12
I am convinced that you can solve this problem with the Visitor pattern.

http://www.dofactory.com/Patterns/PatternVisitor.aspx

No casting is required and you do not need to use the _customNode property,
nor limit yourself to methods that return one object type.

The problem is solved... it is up to you to understand the solution.

--- Nick

"Mike" <mi********@com.nospam> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Nick,

Consider that CustomNode has:

public string Description
public string GetParentDecriptions(CustomNode node)
{
// loop node parents and return a string containing all parent
descriptions
}
"Nick Malik" <ni*******@hotmail.nospam.com> wrote in message
news:Ss2pd.141611$HA.65182@attbi_s01...
Your design problem is not clear. I understand what you are trying to do, but not why, so it is difficult to help.

It is clear that a node has a parent. It is not clear why a childnode

would
need to return anything except a node type for its parent, because this
restricts the entire tree that you can describe.

What design pattern are you attempting to implement? Can you do this

using
the Visitor pattern? (http://home.earthlink.net/~huston2/dp/visitor.html)
--- Nick

"Mike" <mi********@com.nospam> wrote in message
news:e9**************@TK2MSFTNGP14.phx.gbl...
Override does not work when changing the return Type, I want to return
CustomNode which derives from Node.



Nov 16 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: JKJ | last post by:
I'm not an OOP guru, but I would say ". . .not necessarily" What if the base class and derived classes are internal? Then the base class has to be within the same assembly to be inherited from. ...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
6
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself...
15
by: scorpion53061 | last post by:
I am just now really starting to get into inheritance - controls, datasets, views all that stuff and I am seeing the enormous savings in performance and even watching the exe file size go down...
12
by: mijobee | last post by:
I'm very new to c++ and just writing some code to learn. I've run into a problem, with a javaish design, and want to know if there is any possible solution without modifying the design. I've read...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.