If the derived class does not override the method, which version should be called?

I am trying to understand the need for overriding and virtual in C #, so I wrote the following code:

using System; namespace Override { class Base { public virtual void method() { Console.WriteLine("Base method"); } } class Derived : Base { public override void method() { Console.WriteLine("Derived method"); } } class Program { static void Main(string[] args) { Derived d = new Derived(); d.method(); } } } 

And I expected the Derived Method to be called and printed. Then I wrote the following code without using a combination of virtual / overrides.

 using System; namespace Override { class Base { public void method() { Console.WriteLine("Base method"); } } class Derived : Base { public void method() { Console.WriteLine("Derived method"); } } class Program { static void Main(string[] args) { Derived d = new Derived(); d.method(); } } } 

And I got the same result, i.e. called and printed "Derived Method". My question is, does the code work without virtual / overriding, as I expected, what is their need? or am I missing something here?

+6
source share
6 answers

In the source code, you always perform simple inheritance without any polymorphic behavior. You always instantiate a derived class and assign it to an instance variable of the derived class.

 DerivedClass d = new DerivedClass(); // here no polymorphism, and only inheritance is there 

So, when you call a method using a class variable, it will always call the DerivedClass method, regardless of whether this method is virtual or not in the parent class.

In polymorphism, your programs do not know the exact type of class on which you are calling the method (this concept is called late binding). As shown in the example below:

 BaseClass b = new DerivedClass(); // here b is a base class instance but initiated using derived class 

After calling b.method (), it will perform late binding and show polymorphic behavior (only if this method was set virtual in the base class)

NOTE. The virtual keyword delays the binding to the correct version of the method at runtime and is the key key to implementing polymorphism. Therefore, for precise polymorphic behavior, declare the methods as virtual in the parent class, and then in the child class, ovverride this method.

+11
source

virtual allows you to select the correct version of the method at runtime based on the information available at compile time. Consider the following example in your example:

 using System; namespace Override { class Base { public virtual void method() { Console.WriteLine("Base method"); } } class Derived : Base { public override void method() { Console.WriteLine("Derived method"); } } class Program { static void Main(string[] args) { Derived d = new Derived(); Base b = d; b.method(); } } } 

With virtual / override this code will display the Derived method , since at runtime we see that b indeed an instance of Derived . Without virtual / override , the Base method displayed, since the declared type of b is Base .

+8
source

Here is a test that you are missing:

 Base d = new Derived(); d.method(); // "Derived method" Base b = new Base(); b.method(); // "Base method" 

Also imagine if you had a collection of Base objects consisting of different inherited objects. The virtual allows those Base objects to understand what type they really are at runtime.

 List<Base> collection = new List<Base>(); collection.Add(new Base()); collection.Add(new Derived()}; collection.Add(new Base()); foreach(Base b in collection) { b.method(); // will print out "Base" or "Derived" correctly } 
+3
source

see DIFFERENCES

  class Base { public void method() { Console.WriteLine("Base method"); } } class Derived : Base { public void method() { Console.WriteLine("Derived method"); } } class Program { static void Main(string[] args) { Derived d; d = new Derived(); d.method(); d = new Base(); d.method(); } } 

OUTPUT :
Derivative method
Derivative method

  class Base { public virtual void method() { Console.WriteLine("Base method"); } } class Derived : Base { public override void method() { Console.WriteLine("Derived method"); } } class Program { static void Main(string[] args) { Derived d; d = new Derived(); d.method(); d = new Base(); d.method(); } } 

OUTPUT :
Derivative method
Base method

+2
source

Pointers to the base class can be used to indicate the object of the base class or any object obtained from the base. Thus, the need to use virtual methods arises when an object of a base class points to a derived class

 Base d = new Derived(); d.method(); // "Derived method" 
+2
source

The method method in the Derived class will hide the implementation of the Base class, so you get the passed "Derived method".

There are many uses of virtual and abstract, but one example is where you have functionality in a base class that might not correspond to all cases of classes that inherit from your base class. Using virtual allows another class to completely override this functionality and provide its own implementation.

0
source

Source: https://habr.com/ru/post/949565/


All Articles