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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Shivanand ArurPosted Oct 24, 2012, 2:20 AM
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!!!
Rushal AroraPosted Oct 24, 2012, 2:18 PM
Satyapriya NayakPosted Oct 24, 2012, 1:02 PM
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
Shivanand ArurPosted Oct 24, 2012, 7:20 AM
Hey Rushal, can U please mark any of the answer as accepted if it helped U. Thanks...
darma tejaPosted Oct 24, 2012, 6:10 AM
VulpesPosted Oct 24, 2012, 5:38 AM
The output is:
darma tejaPosted Oct 24, 2012, 3:46 AM
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
Santhosh Kumar JayaramanPosted Oct 24, 2012, 1:35 AM
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.