- Override: As we all know, Override implements Polymorphism. Which says I want to use the same method but in various classes with the same signature of methods. So let's see why we need override and how we can use it in our applications.
In the snapshot above you can see I tried to use the same method in two different classes with the same signature. As you can see, the derived class's "Display()" method gave me a Warning. Let's check it.
Now the compiler is confused between the two same methods. So it warns us that use can use the "New" keyword to hide the method above. We will talk later about the New Keyword, now we are talking about Override. So we will solve it using the Override keyword.
Now you can see, I use the two keywords "Virtual" and "Override". Virtual in a base class and override in a derived class. The Virtual keyword is always used in a base class that tells the compiler that this method will be overriden in its derived class. Now try to call them:
In the above snapshot you can see we are calling the methods from both classes, and let's check the output:
As we can see in the above snapshot, using the override keyword we can give a different definition of the derived class's "Display()" method. For Derived class always derived class Display () method will call.
Now a situation develops for us. Like,
I am working on a module and using a DLL (Dynamic Linking Library) that has a method named Display () and I want to use the same method with same signature in my form.
Then how can I use it???
At this situation when I create the Display() method in my form then I must use the new keyword.
- New: "The new keyword hides the definition of the base class method and gives a new definition in the derived class."
You guys have often read this definition but many readers read it and become confused. And say How???
So let's see how
I create an Assembly.
I am using the example of an assembly because; it's the best way for beginners to understand because may be the creator of the Assembly doesn't want other developers to override that method. So when we need to use the new keyword and provide a new definition for that method. See the following:
Here I created a DLL with the name TastBase that contains a class Base and a method Display. Then I built the project. My DLL is ready.
I will now use this DLL in my form.
Add this DLL is in the project and inherits the base class and uses the Display() Method and see what happens:
Here I inherit the base class and use the same method. See, it gave me a warning. Now I will use the new keyword and try to call this method.
Now anyone can guess the output:
Quite interesting
For most users the output should be like.
I am Derived Class Display Method
I am Base Class Display Method
I am Derived Class Display Method
But it's not like that:
Rather than the derived class object, whenever you call this method it will call the Base class method. This new keyword hides the value only for that class.
And out of this class or in another class when you call this method it will call the Base class Display Method.
Monday, August 12, 2019
Difference between Override and New
Subscribe to:
Post Comments (Atom)
No String Argument Constructor/Factory Method to Deserialize From String Value
In this short article, we will cover in-depth the JsonMappingException: no String-argument constructor/factory method to deserialize fro...
-
Hello Guys, How are you all? Hope You all Are Fine. Today I am using webpack But I am facing following error Error: error:0308010C:digital ...
-
Caller Information attributes are newly introduced in Framework 4.0. There are three attributes which are more helpful in tracing, debuggi...
-
Welcome back to the MongoDB article series - Part 4. In the previous article, we discussed the different processes of searching data in Mo...
No comments:
Post a Comment