Similar Posts

Subscribe
Notify of
8 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
TheQ86
1 year ago

A method must virtual be in a subclass override to allow.

One is therefore the prerequisite for the other.

Destranix
1 year ago
Reply to  TheQ86

A method must virtual be in a subclass override to allow.

No, that’s not true. You can also overwrite non-virtual methods.

Both in Java (this is probably the case here), and in C++.

EDIT: (If I am not sure at the latter whether it simply depends on the compiler whether it goes through or whether it is generally allowed. But don’t see a grudn anyway.)

regex9
1 year ago
Reply to  Destranix

It’s about C#. It’s obvious about the syntax, convention and tags on the question.

Only virtual methods can be overwritten. The override-keyword is also used to define abstract methods.

Destranix
1 year ago

I haven’t programmed Java for a long time.

regex9
1 year ago

In Java there is no keyword virtual. 😋

… So I can’t remember having had any problems (…)

In my comment, I am referring entirely to C#.

In Java you do not have an explicit language mark for virtual/override, but can optionally the annotation @Override to let the compiler check if you actually overwrite a method.

In C++ you can read the keyword override use (as in Java optional, since only version 11 is introduced here). If you try to overwrite a method that is not as virtual you only cover it. That is, polymorphic mechanism does not attack as usual.

Example:

#include 

class Base {
  public:
    void doSomething() {
      std::cout << "doSomething (base)" << std::endl;
    }

    virtual void doSomethingVirtual() {
      std::cout << "doSomethingVirtual (base)" << std::endl;
    }
};

class Sub : public Base {
  public:
    void doSomething() {
      std::cout << "doSomething (sub)" << std::endl;
    }

    void doSomethingVirtual() {
      std::cout << "doSomethingVirtual (sub)" << std::endl;
    }
};

int main() {
  Base* sub = new Sub;
  sub->doSomething(); // doSomething (base)
  sub->doSomethingVirtual(); // doSomethingVirtual (sub)

  return 0;
};

If you are override-keyword uses:

class Sub : public Base {
  public:
    void doSomething() override {
      std::cout << "doSomething (sub)" << std::endl;
    }

    /* ... */
};

for this reason you get a compiler error.

error: ‘void Sub::doSomething()’ marked ‘override’, but does not override

Destranix
1 year ago

The tags were missing before.

Syntax would also have been fit for Java.
But the other questions of the questioner read C#, yes.

Only virtual methods can be overwritten. The override-keyword is also used to define abstract methods.

Hm, okay. So I can’t escape to have had any problems when I tried to overwrite non-virtual classes using override. Actually, I use the keyword in C++ as well as in Java. (But I may have lost all the problems that have arisen, as these were easy to fix.)

Destranix
1 year ago

A virtual method cannot be called directly (but it can possibly serve as a fallback if a inherited class does not implement the method).

With “override” you can calibrate methods to overwrite other methods.

LUCKY1ONE
1 year ago

The keywords "override" and "virtual" are used to overwrite a method in a derived class. The difference between the two lies in how the method behaves in the inheritance hierarchy.

A "virtual" method in the basic class (the class from which inherited) can be overwritten by a derived class (the class inherited) by using the keyword "override". A virtual method is defined in the base class and overwritten in the derived class in order to provide a more specific implementation of the method adapted to the class.

Here is an example that illustrates how "virtual" and "override" are used:

 class Tier { //Methode wird definiert public virtual void MacheGeraeusch() { Console.WriteLine("Das Tier macht ein Geräusch"); } } class Hund : Tier //Erbt von der Basisklasse "Tier" { //Methode wird überschrieben public override void MacheGeraeusch() { Console.WriteLine("Der Hund bellt"); } } class Katze: Tier //Erbt von der Basisklasse "Tier" { //Die MacheGeraeusch-Methode wird nicht überschrieben }

Here the outputs, for method call:
—> dog.MakeSound(); = "The dog barks"

—> Cat.MakeSound(); = "The animal makes a noise"

—> Animal.MakeSound(); = "The animal makes a noise"

I think that's quite understandable.

Further information can be found in the Microsoft Docs

Override

Virtual