简体   繁体   中英

How to require an implementation of an abstract class in C#?

I want to build a class that would have a property, in which there is an instance of a class, which implements an abstract class. Here's and example.

public class MyClass {
    public MyDerivedClassA derived;
    public void mainClassUtility () {
        derived.foo();
    }
}

public abstract class MyAbstractBaseClass {
    public abstract void foo();
}

public class MyDerivedClassA : MyAbstractBaseClass {
    public override void foo(){
        return;
    }
}

public class MyDerivedClassB : MyAbstractBaseClass
{
    public override void foo()
    {
        return;
    }
}

Basically, I want to make sure the object I'm using is derived from an abstract class and implements all the methods I will need to use. There will be many implementations of the abstract class and depending on the current state of the program, MyClass might be using different implementations of the ABC. I want to write the program in a way, that no matter what implementation of the ABC is currently being used, there is a way to call it's methods by MyClass. What would be the best solution to this problem?

Unless I'm misunderstanding the question, you're pretty much there. Have MyClass expect a property of the abstract base class and you should be all set.

using System;

public class Program
{
    public static void Main()
    {
        var myClassOne = new MyClass(new MyDerivedClassA());
        var myClassTwo = new MyClass(new MyDerivedClassB());

        myClassOne.mainClassUtility();
        myClassTwo.mainClassUtility();
    }

    public class MyClass 
    {   
        public MyAbstractBaseClass Derived;

        public MyClass(MyAbstractBaseClass derived)
        {
            Derived = derived;
        }

        public void mainClassUtility () 
        {
            Derived.foo();
        }
    }

    public abstract class MyAbstractBaseClass 
    {
        public abstract void foo();
    }

    public class MyDerivedClassA : MyAbstractBaseClass 
    {
        public override void foo()
        {
            Console.WriteLine("I am MyDerivedClassA");
            return;
        }
    }

    public class MyDerivedClassB : MyAbstractBaseClass
    {
        public override void foo()
        {
            Console.WriteLine("I am MyDerivedClassB");
            return;
        }
    }
}

How to require an implementation of an abstract class in C#?

You can not instantiate a abstract class - and thus can not use it for most cases. Except as variable/argument/generic type argument. You need to make a concrete (non-abstract) class that inherits from it. You can only use the abstract class as a variable/argument type. To guarantee that only stuff that inherits from it can be used there.

Basically, I want to make sure the object I'm using is derived from an abstract class and implements all the methods I will need to use.

Then use the abstract class as type argument. It means only instaces of the abstract class (of wich there can be no instance) or instances of classes that inherit from it (that somebody else writes) can be used at that place.

Note that Abstract classes and Interfaces overlap in nearly all uses. There is a miriad small differences, but I do not think they mater. The only big difference I can see, is one of exclusivity:

  • a class can implement as many Interfaces as it wants.
  • You can only inherit from one abstract class. that means it is for a primary, exclusive purpose. That way you prevent some dumb ideas, like someone trying to make a Windows Form that is also a DBConnection.

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