Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to update all the methods of an abstract class?

I need to inherit from a basic abstract class. I want to override only one method. But Visual Studio oblige me to override them all, So I am overriding more than 10 methods that throw NonImplementedException, I find it stupid. Isn't there any way to override only what I need. Or at least to tell Visual Studio to override the rest (non implemented methods and properties)?

The base class is written by the framework so I can't change it (I am talking about RoleProvider of ASP.NET MVC)

like image 740
Younes Ch Avatar asked Feb 18 '13 08:02

Younes Ch


2 Answers

abstract class Base
{
    public void Method1()
    { 
        //some code
    } // No need to override
    public abstract void Method2(); // must be overriden
    public virtual void Method3()
    {
        // some code
    } // Not necessarily be overriden
}

class Derived : Base
{
}

Here the compiler will only ask you to override Method2() as a mandate. It won't ask you to override Method1() or Method3(). You can override Method3() as it bears keyword virtual though.

like image 193
mihirj Avatar answered Sep 22 '22 10:09

mihirj


If the method is abstract , you must override.

If the method is virtual , you can override but not necessarily

like image 37
Zeewon Maharjan Avatar answered Sep 19 '22 10:09

Zeewon Maharjan