How to return an object of the inherited class ?

c#chris

Active member
Joined
Dec 10, 2024
Messages
36
Programming Experience
1-3
Hello,
How can I return an object of the inherited class B from within class A ?
C#:
public class B
{
        //properties of class B
        //methods of class B
}

public class A : B
{
      public A ()
      {
             //constructor fills properties of class A and properties of inherited class B with values.
      }

      public B Get()
      {
            //How can I return an obj that only represents the inhereted class B
            return obj;
      }
}
 
I don't understand the question. Perhaps you have additional constraints that you are not telling us about. As it currently stands, things just work correctly:
C#:
using System;
                   
class BaseClass
{
    public string BaseProperty { get; set; }
}

class DerivedClass : BaseClass
{
    public string DerivedProperty { get; set; }

    public DerivedClass()
    {
        BaseProperty = "b";
        DerivedProperty = BaseProperty.ToUpper();
    }
   
    public BaseClass GetBase()
    {
        return this;
    }
}

public class Program
{
    public static void Main()
    {
        DerivedClass derivedClass = new DerivedClass();
        Console.WriteLine(derivedClass.DerivedProperty);
        Console.WriteLine(derivedClass.BaseProperty);

        BaseClass baseClass = derivedClass.GetBase();
        Console.WriteLine(baseClass.BaseProperty);

        BaseClass baseClass2 = derivedClass;
        Console.WriteLine(baseClass2.BaseProperty);
    }
}

Output:
Code:
B
b
b
b

 
Last edited:
In my case the DerivedClass is returned when I use "this" like in the example above. I also tried "return this as BaseClasse" without success. Unfortunately the whole code is a bit complex, otherwise I would post the code here.
 
But that's the point of inheritance. The entire object is returned even if the return type is a base class (line 18), or the object is assigned to a base class variable (line 35). Once you are accessing the object via the base class variable (lines 33, 36) you can only access the aspects of the derived class that are exposed by the base class. You cannot access the derived class methods. Eg. both of these will fail to compile:
C#:
Console.WriteLine(baseClass.DerivedProperty);
Console.WriteLine(baseClass2.DerivedProperty);

Anyway, I recommend that you post a minimal repro case that shows what you are trying to accomplish.

As an aside, although old fashioned object oriented programming (e.g. from the 80s to the late 90s) encouraged inheritance, more modern object oriented programming encourages the use of composition rather than inheritance. Something like below:
C#:
using System;

interface IBase
{
    string BaseProperty { get; set; }
}

interface IDerived : IBase
{
    string DerivedProperty { get; set; }
}
                    
class BaseClass : IBase
{
    public string BaseProperty { get; set; }
}

class DerivedClass : IDerived
{
    BaseClass _baseClass = new BaseClass();
    
    public string BaseProperty
    {
        get => _baseClass.BaseProperty;
        set => _baseClass.BaseProperty = value;
    }

    public string DerivedProperty { get; set; }

    public DerivedClass()
    {
        _baseClass.BaseProperty = "b";
        DerivedProperty = BaseProperty.ToUpper();
    }
    
    public IBase GetBase()
    {
        return _baseClass;
    }
}

public class Program
{
    public static void Main()
    {
        DerivedClass derivedClass = new DerivedClass();
        Console.WriteLine(derivedClass.DerivedProperty);
        Console.WriteLine(derivedClass.BaseProperty);
        
        IDerived derivedInterface = derivedClass;
        Console.WriteLine(derivedInterface.DerivedProperty);
        Console.WriteLine(derivedInterface.BaseProperty);

        IBase baseInterface = derivedClass.GetBase();
        Console.WriteLine(baseInterface.BaseProperty);

        IBase baseInterface2 = derivedClass;
        Console.WriteLine(baseInterface2.BaseProperty);
    }
}
 
The object is what it is. If you want an object of a different type then you would need to create a new object. Why exactly do you want an object of the base type, as opposed to a reference of the base type to an object of the derived type? What are you actually trying to achieve? I suspect that it is misguided, whatever it is.
 
My understanding (or hope) was, if I inherit B into A and initialize both , the data of B data should be available within obj A like a sepratate obj B and can be returned like an obj B.

Example:
public class B
{
        //properties of class B
        //methods of class B
}

public class A
{
    internal B obj = new();

      public A ()
      {
         //constructor fills properties of class A and properties of obj with values.
      }

      public B Get()
      {
            return obj;
      }
}
 
Yes, you can do that what you have in post #6.

Is the issue that you also want A to expose B properties and methods as if it were part of A? If so, then there is no magic that will do that for you without manually* writing all the wrapper to expose B properties and methods.

The alternative is to break The Law of Demeter and have all those dots:
C#:
A a = new();
a.Get().CallBMethod();
var x = a.Get().BProperty;

*You could use the T4 engine to generate this for you, but if you've never done this before by the time you go up the learning curve, you could have copied-pasted-regex-replaced-written the wrappers."
 
It's still not really clear why you would want to do this. Maybe there's a valid reason but quite possibly not and you're just misunderstanding some principle(s)
 
Back
Top Bottom