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:
((MyBase)myDerived).MyMethod();
)? I know about base
keyword, but it can be called only from the inside of the derived class. Is it right?virtual
and override
modifiers) the method which was called came from the derived class? Would you please explain that?C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With