Senki Sem
Senki Sem

Reputation: 131

How to set the fields of a derived class?

I've 2 classes: "RootClass", and "SubClass". SubClass is derived from RootClass. How do I make a constructor in RootClass, which can set values to fields found in SubClass?

I tried:

Basically I want to have 2 classes. The SubClass is the same as RootClass with a few additional fields, which i cant seem to apply a value to. Depending on a logic I either create a root or sub.

Clearly I'm on the wrong path here, how do I do this?

Upvotes: 0

Views: 221

Answers (6)

Dan J
Dan J

Reputation: 16718

In general, base classes should not have knowledge of their subclasses' implementation details. You should be able to provide your own constructor in the subclass to instantiate its fields, like so:

class SubClass : RootClass
{
    private bool subClassfield;

    public SubClass() : base()
    {
        subClassField = true;
    }
}

Does that not work as expected? Note the : base() expression, which ensures the SubClass's constructor calls the base class's constructor to guarantee the base class is initialized before proceeding.

Upvotes: 1

Jim Mischel
Jim Mischel

Reputation: 134045

Perhaps you're looking for a way for SubClass to call the RootClass constructor?

public class RootClass
{
    public RootClass(int foo)
    {
    }
}

public class SubClass: RootClass
{
    public SubClass(int foo)
    : base(foo)   // calls RootClass constructor
    {
        // now set Subclass fields
    }
}

Upvotes: 2

SAJ14SAJ
SAJ14SAJ

Reputation: 1708

You cannot access any member of the derived class from the root class.

The typical design pattern is to create a method:

virtual void Init(int a, int b) {}

in your base class, where the parameters are whatever information the derived classes would require to perform self-initialization. Call this from the constructor of the root class.

The derived class would then require a constructor

SubClass() : base() { }

which would guarantee the root class constructor is called, and then its own init method is called.


In this case, however, your best design pattern is to simply subclass.

Suppose the RootClass has member int a and the Subclass int b:

class RootClass {

    int a;

    RootClass(int a) {
        this.a = a;
    }
}

class SubClass {

    int b;

    SubClass(int a, int b) : base(a) {
        this.b = b;
    }
}

Upvotes: 1

Rowland Shaw
Rowland Shaw

Reputation: 38130

A class can not know about the implementation of all the classes that inherit from it.

Upvotes: 0

D Stanley
D Stanley

Reputation: 152626

How do I make a constructor in RootClass, which can set values to fields found in SubClass

You can't - as far as RootClass is concerned, SubClass doesn't exist.

Also tried setting the constructor in root to virtual, so the sub constructor may override it

You can't - constructors aren't inherited, so there's no concept of "virtual" constructors.

Here's the typical pattern for "chaining" constructors:

public class RootClass 
{
    public RootClass(int rootProperty)
    {
        this.RootProperty = rootProperty;  
    }

    public int RootProperty {get; set;}
}
public class SubClass : RootClass
{
                                                           v--- call the base constructor
   public SubClass(int rootProperty, string subProperty) : base(rootProperty)
   {
       this.SubProperty = subProperty; 
   }

   public string SubProperty {get; set}
}

Upvotes: 3

rufo
rufo

Reputation: 5577

A base class wouldn't have knowledge of it children classes. What you are saying is either wrong OOP or a very unusual scenario,

Upvotes: 5

Related Questions