which scenario using overriding concept? RRS feed

  • Question

  • User-1070902567 posted

    Overriding means Method name,as well as signature everything is same i known that one,

    My question is which scenario use this approach?

    How can i use this scenario?

    I am not understanding the Overriding concept  

    Thursday, July 17, 2014 1:32 PM


All replies

  • User-1360095595 posted

    Have you read this: http://msdn.microsoft.com/en-us/library/ebca9ah3.aspx

    It has several examples and details of when and why you'd override a method. Give it a read and reply back if you have further specific questions. 

    Thursday, July 17, 2014 2:10 PM
  • User-821857111 posted

    On a practical level, many parts of ASP.NET and Entity Framework provide abstract or virtual methods where you provide an override implementation. The DbContext class in EF has a virtual OnModelCreating method (http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext.onmodelcreating(v=vs.113).aspx). You override it to provide your own customisations, such as mapping relationships for your model.

    The abstract RouteBase class has an abstract GetRouteData method that must be overriden by classes that derive from RouteBase to provide a concrete implementation. I show that here: http://www.mikesdotnetting.com/Article/108/Handling-Legacy-URLs-with-ASP.NET-MVC

    If you create a custom ActionResult deriving from the ActionResult class, you have to override the abstract ExecuteResult method to generate a response like in this article: http://www.mikesdotnetting.com/Article/141/Rss-Feed-Generation-From-a-Custom-RssResult

    If you want to implement your own Custom Validator in the Web Pages framework, you have to inherit from RequestFieldValidatorBase and override the IsValid method to provide your own code to determine what is valid or not. See this article: http://www.mikesdotnetting.com/Article/195/ASP.NET-Web-Pages-Creating-Custom-Validators

    By now you should be getting the idea that overriding is used primarily to provide your own custom code while taking advantage of existing classes and structures. I didn't have to write my own class to work like RequestFieldValidatorBase. I could inherit the existing one that Microsoft already wrote and tested. But because the IsValid method was made virtual, I can provide my own custom implementation through overriding.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 17, 2014 4:10 PM
  • User583311198 posted

    ->Overriding concept can only be used in derived classes.

    ->When we want to give a different implementation for a base class method,  then we can override that method.

    ->The method to be overridden, must be declared as abstract or virtual in the base class.

    ->We can even override a method that is already overridden by the base class in a multi-level inheritance scenario.

    ->Method name, and signature remains the same as in the base class.


     abstract class Shape
            abstract public double Area();
            public Shape() { }
        class Circle : Shape
            public const double PI = Math.PI;
            private double radius;
            public Circle(double radius)
                this.radius = radius;
            public override double Area()
                return PI * radius * radius;
        class Rectangle : Shape
            private double length, breadth;
            public Rectangle(double length, double breadth)
                this.length = length;
                this.breadth = breadth;
            public override double Area()
                return length * breadth;
        class Test
            static void Main()
                Circle c = new Circle(1.2);
                Console.WriteLine("Area of Circle : " + c.Area());
                Rectangle r = new Rectangle(10, 20);
                Console.WriteLine("Area of Rectangle: " + r.Area());

    Tuesday, July 22, 2014 1:45 AM