LearningJrDev
LearningJrDev

Reputation: 941

Inherited class references base class' values

public class ParentClass
{
    public int myId;
    public string commonField1;
    public string commonField2;
    public string commonField3;
    public string changeable;
    public List<ChildClass> children;

    public ParentClass(int id)
    {
        myId = myId;
    }

    public createChildren()
    {
        for(int i = 0; i < 999999; i++) 
        {
            children.Add(new ChildClass(id));
        }
    }
}

public class ChildClass : ParentClass
{
    public ChildClass(int id) : base (id) 
    {
        myId = myId;
        changeable = "new";
    }
}

Because ChildClass only exists in the context of it's ParentClass, and there will be 999999 children. That is creating 999999 copies of all the commonField's when in reality it just needs a reference to it's parent. The only real thing a ChildClass needs to store besides references is changeable.

How can this be accomplished?

I am almost thinking a better approach when I need the children is just to make 999999 shallow copies of the ParentClass and just change the 999999 changeable fields. Shallow copies will have references to commonField1 or would it deep copy values?

Upvotes: 1

Views: 660

Answers (2)

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

If child and parent should have some relation (i.e. common parent class to share methods/properties), but child need to share properties with parent you can redirect properties request to parent (note that it would men getting property of deeply nested child is very slow):

public class Node
{
    public virtual string Field {get {return parent != null ? parent.Field : field;}}

    public Node parent;
    public List<Node> children;

    public Node()
    {
        children = new List<Node>();
    }

    public createChildren()
    {
        for(int i = 0; i < 999999; i++) 
        {
            children.Add(new ChildNode(i, this));
        }
    }
}

public class ChildNode : Node
{
    public override string Field {get {return parent.Field;}}
    public ChildNode(Node parent)
    {
        this.parent = parent;
    }
}

public class RootNode : Node
{
    public override string Field {get {return "Root";}}
    public RootNode()
    {
        this.parent = null;
    }
}

Upvotes: 0

BradleyDotNET
BradleyDotNET

Reputation: 61339

You have one thing right, thats a lot of repeated data if commonFieldX is truly unchanging.

I see several solutions:

  1. Prefer Composition over Inheritance

    Why are you inheriting at all? If there is no polymorphic behavior, then just pass the base class instance to the 10000 children and call it good:

    public createChildren()
    {
        for(int i = 0; i < 999999; i++) {
             children.Add(new ChildClass(id, this));
        }
    }
    
    public class ChildClass
    {
         public ChildClass(int id, ParentClass parent) : base (id) 
         {
               myId = myId; //This shouldn't be part of the base class?
               changeable = "new"; //Same here
               myParent = parent;
         }
    }
    
  2. Share those variables among all instances

    static members belong to the "global" instance, so they won't be recreated for each derived object:

    public class ParentClass
    {
        public int MyId {get; set;}
        public static string CommonField1 {get; set;}
        public static string CommonField2 {get; set;}
        public static string CommonField3 {get; set;}
        public string Changeable {get; set;}
    

    Note that some of this should likely be protected instead of public and you should always expose public fields via properties instead of directly. Of course, if you have multiple instances of ParentClass that have differing values of those fields, this is a no-go.

Upvotes: 1

Related Questions