Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overridable and Override in C# and VB

Tags:

c#

.net

oop

In C#, override is enabled by default so, is there no need to explicitly declare a method as overridable in the base class? If so

  1. Is Overridable just limited to VB.NET or it is required in C# as well?
  2. Hence which type of methods can be overridden? Is it only abstract methods of an abstract class or any method?
like image 416
RKh Avatar asked Jul 19 '12 05:07

RKh


People also ask

What is override in C?

When a derived class or child class defines a function that is already defined in the base class or parent class, it is called function overriding in C++. Function overriding helps us achieve runtime polymorphism.

Can a function be both virtual and override?

The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual. Virtual functions in a base class must be defined unless they are declared using the pure-specifier.

What is difference between virtual and override in C#?

The virtual keyword is used to modify a method, property, indexer, or event declared in the base class and allow it to be overridden in the derived class. The override keyword is used to extend or modify a virtual/abstract method, property, indexer, or event of base class into a derived class.

What is the difference between new and override in C++?

The override modifier extends the base class virtual method, and the new modifier hides an accessible base class method. The difference is illustrated in the examples in this topic. In a console application, declare the following two classes, BaseClass and DerivedClass .


2 Answers

The Overridable keyword in VB corresponds to the virtual keyword in C#.

You have to make a method virtual to be able to override it. Abstract methods are automatically virtual.

like image 98
Guffa Avatar answered Oct 19 '22 13:10

Guffa


In C#, any method marked as 'virtual' can be overridden. Methods marked as 'abstract' are not necessarily overridden, they are implemented in classes that implement the abstract class. They can be marked as virtual in the implementation. There is no limit to the number of times a virtual method can be overridden.

Do you need an answer for VB.NET?

like image 33
Glenn Ferrie Avatar answered Oct 19 '22 13:10

Glenn Ferrie