Hi Everyone,
Please let me know the solution of below question :
Q.1) Need of Overriding?
Thanks and Regards
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Shivanand ArurRank #920 · 2.3K reputationPosted Oct 23, 2012, 10:20 PM EDT
What is the use of Overriding???
Basically Overriding helps you to write less code by inheriting a parent class and "Reuse" the methods defined in the Parent class. For overriding a method, you first have to define the method in the Parent class with a "Virtual" keyword.
Now let me give you a simple example!!!
public class ParentClass()
{
public virtual void Walk()
{
Console.WriteLine("The Object is walking");
}
public virtual void Run()
{
Console.WriteLine("The Object is running");
}
}
Now a Human being can walk and run, let it be a male or a female. Even Animals walk and run. But the functioanlity of doing those things is different (2 legged Object(Man/Woman) and 4 legged Object(Animal))
public class Animal : ParentClass
{
public override void Run()
{
Console.WriteLine("An Animal runs using its 4 legs");
}
}
So as you can see, the functionality changed but by using the Virtual and override keyword the Child Class can make use of the Parent Class's functions...
Hope this helps you out.
Please mark the answer as accpeted, if it helped!!!
Thank you!!!
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Oct 23, 2012, 9:35 PM EDT
Now when you have child class circle, it will also has same method, but the implementation i.e formula/calculation will be different. Similarly for sphere, square. So there will be a common method calcualte area for most of the shapes, but for some of the child classes can have their own area calculation logic. So we need to go for overriding.
darma tejaRank #3,199 · 449 reputationPosted Oct 23, 2012, 11:46 PM EDT
I did your example without keyword "virtual" and "override" and it works.
Is it ok if i dont use "virtual" and "override" keywords in overriding.
Darma
VulpesRank #55 · 29.2K reputationPosted Oct 24, 2012, 1:38 AM EDT
The output is:
darma tejaRank #3,199 · 449 reputationPosted Oct 24, 2012, 2:10 AM EDT
Shivanand ArurRank #920 · 2.3K reputationPosted Oct 24, 2012, 3:20 AM EDT
Hey Rushal, can U please mark any of the answer as accepted if it helped U. Thanks...
Satyapriya NayakRank #47 · 31.5K reputationPosted Oct 24, 2012, 9:02 AM EDT
Overriding: - It is the characteristics of the child or derived class, which overrides the characteristics of the parent class. The procedure in the base class, which could be overrides, is prefixed with the keyword Overridable. The procedure in the child class, which Overrides the base class procedure, is prefixed with the keyword overrides.
Mybase keyword is used in the child class to call the parent class procedure.
Program
Public Class Form2
Dim a As New sagar1
Dim b As New sagar2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(a.amit(10, 20))
MsgBox(b.amit(10, 20))
End Sub
Public Class sagar1
Public Overridable Function amit(ByVal x As Integer, ByVal y As Integer)
Dim z As Integer
z = x + y
Return z
End Function
End Class
Public Class sagar2
Inherits sagar1
Public Overrides Function amit(ByVal x As Integer, ByVal y As Integer)
Dim z As Integer
z = MyBase.amit(x, y) * 5
Return z
End Function
End Class
End Class
Thanks
Rushal Arora191 reputationPosted Oct 24, 2012, 10:18 AM EDT