Day 73: Virtual & Override

Virtual keyword is used for generating a virtual path for its derived classes on implementing method overriding” was how one article explained it…which helped not even the slightest. Onto a different article:

A virtual method is a method that can be redefined in derived classes. It is used when a method’s basic functionality is the same but sometimes more functionality is needed in the derived class. A virtual method is created in the base class that can be overriden in the derived class.”

Okay, that makes more sense. But now I need to look into how a virtual method differs from an abstract one. On the surface they both look like they function similarly with one main difference: an abstract method has to be overriden in it’s derived class whereas a virtual method can remain as-is or be overriden. I think.

I’ll need to do more reading on this topic tomorrow.

Current Progress Status

  • Tower Defense Tutorial: 67/73
  • C# The Yellow Book (pg 127/216)

Onward!

Author: Mike@PAUSED

Aspiring indie game developer. Devlog: www.paused.ca

5 thoughts on “Day 73: Virtual & Override”

  1. In my experience, you won’t use virtual or abstract that much unless you get into library or framework building. Don’t get me wrong you should still know them, however, I wouldn’t stress their knowledge at this stage of your learning. Just my 2 cents. Now on to the example. 🙂

    Fiddle: https://dotnetfiddle.net/F8Kp41

    The two concepts are closely related. Essentially the abstract method does NOT have a base implementation and the virtual one DOES. The abstract method MUST be overriden (or hidden by new) and the virtual method MAY be overriden (or hidden).

    Off the top of my head I see uses for them in an RPG game, stats, equipment, monsters. I think equipment is the easiest one to reference. All equipment takes up space, so a base class could have an method to return the amount of space required for that piece of equipment. If you want all equipment to take up a minimum of 1 space you use a virtual method in the base class, then in the derived classes that also take up 1 you don’t have to do anything. This saves you time and effort.

    If you’re not sure about the min space, then use abstract (in an abstract class) and force the derived classes to implement that method. This could be useful if you want to make gold pieces act as equipment but take up no space.

    Ready for the real kicker? I can think of a dozen different ways to implement what I just described and they are all correct. Hence the analysis paralysis sometimes. In the end, you need to find the ways that work best for what you want to achieve. Sometimes, that’s a quick and easy solution, and sometimes that a base class with lots of virtual and abstract methods.

    Remember if you get 5 developers trying to solve a problem you will get at least 15 different solutions. 🙂

    Liked by 1 person

    1. Would you be able to very briefly explain 1 or 2 different ways you would implement that? It would be really helpful to see how to tackle a programming task in multiple ways

      Liked by 1 person

          1. C# doesn’t allow multiple class inheritance as it lead to a number of problems in other languages. Instead MS decided to create an interface (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/interface), which is not a class, but has the elements of a light-weight class. More importantly, a class can inherit from multiple interfaces at a time.

            In some class libraries you will see an interface defining methods, then an abstract class implementing that interface, and adding some definitions to the methods. Then a “real” class that implements the abstract class.

            Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: