Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between new and override?

Tags:

c#

overriding

I have a base class which I want to provide some 'base' functionality for methods for all inheriting classes.

In my inheriting classes I want to do:

public override void Setup()
{
   base.Setup();
}

But at the moment it says I have to use the new keyword.

How to I have it so I have to use the override keyword?

Is there any difference between how it is currently with me using the new keyword and using override?

like image 495
Blankman Avatar asked Sep 24 '11 22:09

Blankman


People also ask

What is the difference between 'New' and 'override' methods?

Now on calling methods will take its state into consideration. Override: means that it extends the function of the method, then it uses the method in the derived class, whereas new tell the compiler to hide the method in the derived class and use the method in the base class instead. Here is a very good sight to that subject:

What is the difference between the new and override keywords?

You can specify how the methods interact by using the new and override keywords. 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.

What is the difference between virtual and override in Java?

Note 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 derived class.

How do you override a method in a class?

override: virtual keyword must be defined to override the method. The method using override keyword that regardless of reference type (reference of base class or derived class) if it is instantiated with base class, the method of base class runs. Otherwise, the method of derived class runs.


2 Answers

It says so because you have not declared the base method virtual. Once you do so, the base virtual/derived override pair of keywords will make sense. Right now, the compiler is complaining that you cannot override a non-virtual method.

like image 131
Jon Avatar answered Sep 26 '22 14:09

Jon


When you use the override keyword the derived method is used even when the instance is passed as a Base class type. Hence there needs to be a virtual in the base class so that the programme knows it has to make a runtime check of what the actual type of the instance is. It then looks up what the actual type is and if it is a derived type it checks if their is an override in the derived class for that particular method in the base class.

Member hiding is different. The base member will only be hidden if it is actually passed as an instance of the derived class. If the object is passed as a base class, the base class method will still be used. If you hide a member, you get a warning if you haven't use the new keyword. The new keyword is merely to tell the complier, that you really do mean to hide the base type method.

like image 31
Rich Oliver Avatar answered Sep 23 '22 14:09

Rich Oliver