C# Hiding, overriding and calling function from base class
Asked Answered
P

4

16

I'm learning C# and I encountered the following problem. I have two classes: base and derived:

class MyBase
{
    public void MyMethod()
    {
        Console.WriteLine("MyBase::MyMethod()");
    }
}


class MyDerived: MyBase
{
    public void MyMethod()
    {
        Console.WriteLine("MyDerived::MyMethod()");
    }
}

For now, without virtual and override key words. When I compile this I get the warning (which is of course expected) that I try to hide MyMethod from MyBase class.

What I want to do is to call the method from the base class having an instance of derived class. I do this like this:

MyDerived myDerived = new MyDerived();
((MyBase)myDerived).MyMethod();

It works fine when I do not specify any virtual, etc. keywords in the methods. I tried to put combination of the keywords and I got the following results:

| MyBase::MyMethod | MyDerived::MyMethod | Result printed on the console |
| -----------------|---------------------|-------------------------------|
| -                | -                   | MyBase::MyMethod()            |
| -                | new                 | MyBase::MyMethod()            |
| virtual          | new                 | MyBase::MyMethod()            |
| virtual          | override            | MyDerived::MyMethod()         |

I hope the table is clear to you. I have two questions:

  1. Is it the correct way to call the function from the base class (((MyBase)myDerived).MyMethod();)? I know about base keyword, but it can be called only from the inside of the derived class. Is it right?
  2. Why in the last case (with virtual and override modifiers) the method which was called came from the derived class? Would you please explain that?
Patricapatrice answered 23/3, 2010 at 12:36 Comment(1)
Can you please elaborate on what you want and not what you experienced? All I can say right now is that "Yes, that looks correct". For the last question there you really should read up on how virtual method dispatching works. See en.wikipedia.org/wiki/Virtual_method_table for information.Semitropical
C
20

When you call a virtual method on an instance of a type that overrides the method, the overridden version will always be called, even if you cast to the base class.

The only way to call the base implementation of a virtual method on a class that overrides the method is to make a second method in the derived class (not the base class) that calls the method using the base keyword.

In general, needing to do this is a sign of a poor API design - if you think you'll need to call the base version, the derived version should probably have a different name.

Caveator answered 23/3, 2010 at 12:38 Comment(0)
H
3

You're correct - base can only be called from within the derived class - Source.

This page also gives an example of how to override the base class definition.

Hord answered 23/3, 2010 at 12:40 Comment(0)
N
3

As for your second question, you are not changing the type of the object you have a reference to, just the interface you are referencing it through. So if you have an object B that inherits from A and overrides function C, even if you refer to B as an A, it still calls the implementations of the most derived type, in this case B.

Nocti answered 23/3, 2010 at 12:42 Comment(0)
H
2
  1. You're right, base refers to the base class for a given instance.
  2. The involved mechanism is called polymorphism : you should better work without warning. The natural object oriented good way is the last of the cases you mentioned.

By the way, try to avoid writing code that enforces the substitution principle, in other word, don't write code that depends on the implementation of your class hierarchy because you'll have to modify this code if you add a new derived class to your base class.

Hierarch answered 23/3, 2010 at 12:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.