views:

1898

answers:

4

In the code below I tried in two ways to access the parent version of methodTwo, but the result was always 2. Is there any way to get the 1 result from a ChildClass instance without modifying these two classes?

class ParentClass
{
    public int methodOne()
    {
        return methodTwo();
    }

    virtual public int methodTwo()
    {
        return 1;
    }
}

class ChildClass : ParentClass
{
    override public int methodTwo()
    {
        return 2;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var a = new ChildClass();
        Console.WriteLine("a.methodOne(): " + a.methodOne());
        Console.WriteLine("a.methodTwo(): " + a.methodTwo());
        Console.WriteLine("((ParentClass)a).methodTwo(): "
   + ((ParentClass)a).methodTwo());
        Console.ReadLine();
    }
}

Update ChrisW posted this:

From outside the class, I don't know any easy way; but, perhaps, I don't know what happens if you try reflection: use the Type.GetMethod method to find the MethodInfo associated with the method in the ParentClass, and then call MethodInfo.Invoke

That answer was deleted. I'm wondering if that hack could work, just for curiosity.

+18  A: 

Inside of ChildClass.methodTwo(), you can call base.methodTwo().

Outside of the class, calling ((ParentClass)a).methodTwo()) will call ChildClass.methodTwo. That's the entire reason why virtual methods exist.

Craig Stuntz
+1 polymorphism in all its glory
Andrew Hare
.., and -1 for all the ways it gets overused/misused.
Michael Meadows
Michael - you downvoted someones explanation of polymorphic behavior? Craig was simply explaining why things work the way that they do, don't flail about downvoting good answers simply because they explain a concept you happen to believe is overused.
Andrew Hare
I'm guessing he was joking. :)
Craig Stuntz
didn't downvote. just wanted to throw in my -1 cent on the overuse of polymorphism. Sorry if that wasn't clear.
Michael Meadows
A: 

To my knowledge, once a method has been overridden then you can't call the parent method.

Sir Psycho
This is not a bad answer - I think he meant you can't call the overriden parent method from an instance of the child, which was the spirit of the question.
Andrew Hare
+11  A: 

At the IL level, you could probably issue a call rather than a callvirt, and get the job done - but if we limit ourselves to C# ;-p (edit darn! the runtime stops you: VerificationException: "Operation could destabilize the runtime."; remove the virtual and it works fine; too clever by half...)

Inside the ChildClass type, you can use base.methodTwo() - however, this is not possible externally. Nor can you go down more than one level - there is no base.base.Foo() support.

However, if you disable polymorphism using method-hiding, you can get the answer you want, but for bad reasons:

class ChildClass : ParentClass
{
    new public int methodTwo() // bad, do not do
    {
        return 2;
    }
}

Now you can get a different answer from the same object depending on whether the variable is defined as a ChildClass or a ParentClass.

Marc Gravell
+1 for the clever hack
Andrew Hare
nice hack! = upvotednice answer ("this is not possible externally.") = chosen
Jader Dias
A: 

I would think that it is not possible unless you make instance of the ParentClass directly. Thats the very essense of inheritance, polymorphism...

J Angwenyi