简体   繁体   中英

How to avoid implementing methods from abstract class?

For simplicity purposes, let's say I have a "Base" abstract class:

public abstract class BaseClass
{
    public abstract void BaseMethod(object obj);

    public abstract void BaseMethod();
}

I want to make a 2nd and 3rd class inherit from this "BaseClass":

public class Class1 : BaseClass
{
    public override void BaseMethod(object obj)
    {
        //Some code
    }
}

public class Class2 : BaseClass
{
    public override void BaseMethod()
    {
        //Some code
    }
}

Basically, I want each of the classes to implementing one method without implementing the other . The example above doesn't work, since for each class I'm prompted to implement the other abstract method.

I'm aware I can just implement the other method 'empty'. However, I'm trying to avoid this since I've found the following statement:

Abstract classes can be inherited without implementing the abstract methods (though such a derived class is abstract itself)

Is this statement wrong? Any input on this topic is deeply appreciated. Thanks!

Abstract classes can be inherited without implementing the abstract methods ( though such a derived class is abstract itself )

The bold part is the part you're not complying with - yes you can have an "intermediate" class that only implements some of the abstract methods, but that class must be abstract as well . At some point, when you get to the concrete class, all abstract methods must be implemented; either in the class itself or somewhere up the inheritance chain.

The statement is not wrong, however to follow the statement your code needs to be changed to

public abstract class Class1 : BaseClass
{
    public override void BaseMethod(object obj)
    {
        //Some code
    }
}

public abstract class Class2 : BaseClass
{
    public override void BaseMethod()
    {
        //Some code
    }
}

So you still will not be able to create the classes. A better solution would be make the functions virtual instead of abstract with a empty body or a body that throws a error.

public abstract class BaseClass
{
    public virtual void BaseMethod(object obj) { throw new NotImplmentedException(); }

    public abstract void BaseMethod() { throw new NotImplmentedException(); }
}

But I still think this is a bad idea, if you have a List<BaseClass> how will you know which is the correct version you will want to call? You should re-think your design to only have one virtual method that both functions can use.

"Abstract classes can be inherited without implementing the abstract methods (though such a derived class is abstract itself)".

This statement is correct. It means that the classes 'Class1' and "Class2" would also have to be abstract if it is not implementing all the abstract methods.

As said by others you can't. You have to divide your BaseClass in two different classes (if BaseClass is yours).

OR

you can "cheat" by wrapping or, as in the example, extending BaseClass in a certain way

    public class BaseClassWrapper : BaseClass
    {
        private IBaseMethodA methodA;

        private IBaseMethodB methodB;

        public BaseClassWrapper(IBaseMethodA methodA, IBaseMethodB methodB)
        {
            this.methodA = methodA;
            this.methodB = methodB;
        }

        public override void BaseMethod()
        {
            methodB.BaseMethod();
        }

        public override void BaseMethod(object obj)
        {
            methodA.BaseMethod(obj);
        }

        public interface IBaseMethodA
        {
            void BaseMethod(object obj);
        }

        public interface IBaseMethodB
        {
            void BaseMethod();
        }
    }

Now you have the two separate interface to implement. This clearly make the design and implementation more complex but might give you some advantages. For example if you have 4 different implementation for IBaseMethodA and 3 for IBaseMethodB you can mix and match them.

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