简体   繁体   中英

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)

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.

If the method is abstract , you must override .

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM