0

I did some searching and had trouble finding anything that specifically addressed calling a parent method using a child object instead of calling a child method using a parent object specifically for c#.

Is there was a way to make a call to a parent method using an object made using a child class' constructor? For example say you have a parent and child class that looks like this:

public class Parent
{
   private string name;

   public A(string n)
   {
      name = n;
   } 

   public virtual string GetInfo()
   {
      return name;
   }
}
public class Child : Parent
{
    private string lname;
    public Child (string n, string ln)
    : base(n)
    {
         lname = ln; 
    }
    public override string GetInfo()
    {
         return base.GetInfo + " " + lname;
    }
}

If you were to create a Child object with the appropriate call in Main would it be possible for you to call the GetInfo() method from the parent class? I'm still fairly new to programming, but would this be accomplished by something like casting the Child object as a Parent? If so, how would I go about doing that? Would something like (Parent)child.GetInfo(); be the appropriate call assuming child was created with the Child constructor?

badslinkie
  • 23
  • 6
  • Have you tried it? Do what you're asking at the end of your question, and see what results you get. – krillgar Nov 14 '16 at 18:28
  • Ahh okay, I didn't know that this was referred to as reflection, so that made it tough to find in my searches. Thank you! – badslinkie Nov 14 '16 at 18:32

0 Answers0