6

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)

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
Younes Ch
  • 325
  • 2
  • 4
  • 14
  • 2
    You should only need to implement those that do not have a default implementation. If you want you can add a default implementation to your base class which return the exception. – Karthik T Feb 18 '13 at 08:45
  • 1
    If you won't use any other method then you can just leave dummy `throw new NonImplementedException();` method bodies. – default locale Feb 18 '13 at 08:50
  • 1
    Someone gave a down vote, but I change to up. Looks like that here in Stackoverflow is not allowed have a basic doubt. – Andre Araujo May 11 '18 at 18:55

6 Answers6

18
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.

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
mihirj
  • 1,199
  • 9
  • 15
7

If the method is abstract , you must override.

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

Zeewon Maharjan
  • 107
  • 2
  • 6
5

The real problem here is that you have a base class with so many methods that a derived class can work only with a subset of them. This means that your base class most likely has multiple responsibilities and thus violates the Single Responsibility Principle (SRP).

The solution is to split your base class into several smaller classes where each of them has exactly one responsibility.


If the base class is not from you, you really need to implement all of those methods.
If the base class is a class that violates the SRP and your implementation can really work correctly if you implement only a small subset of the methods you could create an abstract base class deriving from that other abstract base class. In your abstract base class you can implement all methods you don't need and throw a NotImplementedException. Don't implement those methods you need.
Now, derive a class from your base class - you now only have to implement the methods you are interested in. Be sure to document this properly.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
2

Isn't there any way to override only what i need.

No, that's how abstract classes work. If you make your class abstract as well you don't need to implement all methods.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

It is necessary to override all the methods which are declared as abstract. you cannot skip the non-concrete methods. If you really want to do then make your class as abstract. you cannot change the mechanism of abstraction.

Remember, abstract classes have the following features:

  1. An abstract class cannot be instantiated.
  2. An abstract class may contain abstract methods and accessors.
  3. It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited.
  4. A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
asifsid88
  • 4,631
  • 20
  • 30
0

You could also create an abstract class which would inherit and implement all methods (with NotImplementedException), and then your subclasses could inherit that abstract class and implement only the methods that are relevant for you.