1

I have the following code:

public partial class Root : ICustomInterface
{
    public virtual void Display()
    {
        Console.WriteLine("Root");
        Console.ReadLine();
    }
}
public class Child : Root
{
    public override void Display()
    {
        Console.WriteLine("Child");
        Console.ReadLine();
    }
}
class Program
{
    static void Main(string[] args)
    {
        Root temp;
        temp = new Root();
        temp.Display();
    }
}

Output: "Root"
Desired output: "Child"

When I instantiate a Root object and call the Display() method I want to display the overridden method in Child is this possible.

I need this because I must create a plugin that's an extension to the base code and voids the Display() method of the Root class and implements only the plugin's method Child

XandrUu
  • 1,169
  • 4
  • 26
  • 46
  • 3
    I don't think you fully understand inheritance. you need to create an instance of Child if you want the Display() method to output "Child" – Dave Lawrence Apr 04 '13 at 09:11
  • 1
    For your edited part of question you should see : http://stackoverflow.com/questions/2779146/c-is-there-way-for-a-class-to-remove-methods-that-it-has-inherited and also this one: http://stackoverflow.com/questions/1125746/how-to-hide-remove-a-base-classs-methods-in-c – Habib Apr 04 '13 at 09:25

5 Answers5

6

When I instantiate a Root object and call the Display() method I want to display the overridden method in Child is this possible.

You need to create instance of the Child class.

Root temp;
temp = new Child(); //here
temp.Display();

Currently your object temp is holding reference of the base class, it doesn't know anything about the child, hence the output from the base class.

Habib
  • 219,104
  • 29
  • 407
  • 436
2

When I instantiate a Root object and call the Display() method I want to display the overridden method in Child is this possible.

No. Suppose you add another class:

public class Child2 : Root
{
    public override void Display()
    {
        Console.WriteLine("Child 2");
        Console.ReadLine();
    }
}

Then which method (Child.Display() or Child2.Display()) would you expect to be called for a Root instance?

H H
  • 263,252
  • 30
  • 330
  • 514
1

It is not possible with your current code because you are creating a Root instance and not a Child instance. Therefore it has no idea about the Display method inside Child.

You'll need to create a Child class:

Root temp;
temp = new Child();
temp.Display();
Arran
  • 24,648
  • 6
  • 68
  • 78
1

This is not how OOP works. You cannot use an overridden method in a base class. If you do this:

static void Main(string[] args)
{
    Root temp;
    temp = new Child();
    temp.Display();
}

You should get your desired output.

Elad Lachmi
  • 10,406
  • 13
  • 71
  • 133
1

No it's not possible you have to instantiate a Child object instead of a root

Root temp;
temp = new Child();
temp.Display();

if you don't want to modify temp then you have to modify Root display method to print "child" instead of root

Fabio Marcolini
  • 2,315
  • 2
  • 24
  • 30
  • I don't want to modify the temp object. – XandrUu Apr 04 '13 at 09:12
  • I don't want to do that I want to create a plugin and not modify the base code of the project, the plugin must be an extension that voids the Root method and calls only my Child method. – XandrUu Apr 04 '13 at 09:16
  • 1
    That's impossible, if the object is a Root and Root display "Root". How are you gonna modify the output if you can't modify the code? – Fabio Marcolini Apr 04 '13 at 09:19