I have a base and derived class called Node which form elements of a Graph.
I’d like to define a DerivedGraph that can only store DerivedNodes and may have some DerivedNode specific Methods. The Nodes must be mutable in both classes.
What is the standard practice for dealing with this?
Duplicating data members is clearly to be avoided.
Should it be done by type checking and casting within the properties?
public class Node {}
public class DerivedNode : public Node {}
public class Graph
{
private List<Node> nodes;
//some code
}
class DerivedGraph : Graph {}
EDIT:
I should add I need the functionality that Graph can contain both Nodes and DerivedNodes
I would suggest the use of Generics to solve this problem.
This would allow you to specify either
NodeorDerivedNodewhen instantiating the class.