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.
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
Lakshmanan Sethu SankaranarayanRank #124 · 17.7K reputationPosted May 10, 2014, 7:56 AM EDT
Abhay ShankerRank #375 · 6.7K reputationPosted May 10, 2014, 8:03 AM EDT
http://msdn.microsoft.com/en-us/library/ebca9ah3.aspx
Abhishek KumarRank #510 · 4.8K reputationPosted May 10, 2014, 8:45 AM EDT
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
Joe WilsonRank #631 · 3.8K reputationPosted May 11, 2014, 4:15 AM EDT
by the way, could you please give me a few examples?
Joe WilsonRank #631 · 3.8K reputationPosted May 11, 2014, 4:17 AM EDT
Abhishek JaiswalRank #126 · 17.5K reputationPosted May 11, 2014, 7:36 PM EDT
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/
:)
Abhishek KumarRank #510 · 4.8K reputationPosted May 11, 2014, 9:19 PM EDT
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.
Anupam SinghRank #643 · 3.7K reputationPosted May 11, 2014, 9:42 PM EDT
Joe WilsonRank #631 · 3.8K reputationPosted May 19, 2014, 12:12 PM EDT