For Example , What is the usage of override.
public override string To String()
{return String.Format("Name = {0}, Age = {1}", Name, Age);
}
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.
Joe WilsonPosted May 19, 2014, 4:12 PM
Anupam SinghPosted May 12, 2014, 1:42 AM
Abhishek KumarPosted May 12, 2014, 1:19 AM
Override is explained by below example.
Taking an example of simple requirement , suppose we need to calculate area of different shapes which has different formula for calculating area.
public abstract class shapes
{
// Abstract Method
public abstract void calculateArea();
}
here i have made calculate area is abstract method because derived shape class will implement it different way.
here is first shape Square.
public class sqaure : shapes
{
public float Area;
public float length;
public override void calculateArea()
{
// provide implementation
Area = (length * length);
Console.WriteLine("Sqaure Area : " + Area);
}
}
our 2nd example shape is circle which has Area is being calculated by formula Pi*R*R
where r is radius of circle.
public class Circle : shapes
{
public double Area;
public float radius;
public const double Pi = 3.14;
public override void calculateArea()
{
// provide implementation
Area = (Pi* radius * radius);
Console.WriteLine("Circle Area : " + Area);
}
}
class Program
{
static void Main(string[] args)
{
sqaure sq = new sqaure();
sq.length=10;
sq.calculateArea();
Circle c = new Circle();
c.radius = 10;
c.calculateArea();
Console.ReadLine();
}
}
so basically Square and circle has override the abstract calculateArea method and implemented the formula to calculate the area of that particular shape.
Hope this make senses.
Abhishek JaiswalPosted May 11, 2014, 11:36 PM
have a look at these links
http://www.dotnet-tricks.com/Tutorial/csharp/U33Y020413-Understanding-virtual,-override-and-new-keyword-in-C
http://www.c-sharpcorner.com/UploadFile/2072a9/method-overriding-in-C-Sharp/
:)
Joe WilsonPosted May 11, 2014, 8:17 AM
Joe WilsonPosted May 11, 2014, 8:15 AM
by the way, could you please give me a few examples?
Abhishek KumarPosted May 10, 2014, 12:45 PM
override is modifier which will be used when ever we want to change the method implementation in derived class for abstract or virtual methods from base class.
In Above example tostring is the method which is used to convert object into string.
By overriding the functionality here we are trying to format string by extending the tostring() method.
Hope this will help.
Abhishek
Abhay ShankerPosted May 10, 2014, 12:03 PM
http://msdn.microsoft.com/en-us/library/ebca9ah3.aspx
Lakshmanan Sethu SankaranarayanPosted May 10, 2014, 11:56 AM