How to force sub classes to implement a method

C#OopInheritance

C# Problem Overview


I am creating an object structure and I want all sub classes of the base to be forced to implement a method.

The only ways I could think of doing it were:

  1. An abstract class - Would work but the base class has some useful helper functions that get used by some of the sub classes.

  2. An interface - If applied to just the base class then the sub classes don't have to implement the function only the base class does.

Is this even possible?

N.B. This is a .NET 2 app.

C# Solutions


Solution 1 - C#

You can have abstract methods in a class with other methods that are implemented. The advantage over an interface is that you can include some code with your class and have the new object be forced to fill in the details for the abstract methods.

public abstract class YourClass
{
    // Your class implementation

    public abstract void DoSomething(int x, int y);

    public void DoSomethingElse(int a, string b)
    {
        // You can implement this here
    }
}

Solution 2 - C#

> An interface - If applied to just the > base class then the sub classes don't > have to implement the function only > the base class does.

This is not entirely correct. If the base class is abstract, you can mark methods that belong to the interface as abstract, and force the implementation in the subclasses.

That brings an option you didn't mention: to use both. You have an IFoo interface, and a FooBase abstract base class the implements it, or part of it. This provides subclasses with a "default" implementation of the interface (or part of it), and also lets you inherit from something else and still implement the interface, or if you want to implement the interface but not inherit the base class implementation. An example might help:

// Your interface
interface IFoo { void A(); void B; }

// A "default" implementation of that interface
abstract class FooBase : IFoo
{
    public abstract void A();

    public void B()
    {
        Console.WriteLine("B");
    }
}

// A class that implements IFoo by reusing FooBase partial implementation
class Foo : FooBase
{
    public override void A()
    {
        Console.WriteLine("A");
    }
}

// This is a different class you may want to inherit from
class Bar
{
    public void C()
    {
        Console.WriteLine("C");
    }
}

// A class that inherits from Bar and implements IFoo
class FooBar : Bar, IFoo
{
    public void A()
    {
        Console.WriteLine("Foobar.A");
    }
    public void B()
    {
        Console.WriteLine("Foobar.B");
    }
}

Solution 3 - C#

> An abstract class - Would work but the > base class has some useful helper > functions that get used by some of the > sub classe

An abstract class doesn't require all functions it provides to be abstract.

abstract class Base {
    public void Foo() {} // Ordinary method
    public virtual void Bar() {} // Can be overridden
    public abstract void Xyz(); // This one *must* be overridden
}

Note that if you replace public with protected, the marked method will be only visible to base classes and subclasses.

Solution 4 - C#

Yes, and if all the classes you need to do this for are logically subclasses of an existing abstract base class, then add an abstract method to the base class... This is better than an interface because it allows you to add implementation later (by changing abstract base class method to virtual method with a default implementation), if/when it turns out that, say, eight of ten derived classes will have the same implementation, and say, only two of them differ...

EDIT: (based on thread in comments below) The base class must be declared as abstract to do this... You can't have an abstract method in a non-abstract class because a non-abstract class can be instantiated, and if an instance of it was created, there wouldbe NO implementation for that method. So this is illegal. By declaring the base as abstract, you inhibit instantiation of the class. Then, only non-abstract derived classes can be instantiated, where, (because the base method is abstract) you MUST add an implementation for that method.

Solution 5 - C#

And full worker sample with params (.netcore 2.2):

class User{
    public string Name = "Fen";
}

class Message{
    public string Text = "Ho";
}

// Interface
interface IWorkerLoop
{
	// Working with client message
	string MessageWorker(string msg);
}

// AbstractWorkerLoop partial implementation
public abstract class AbstractWorkerLoop : IWorkerLoop
{
    public User user;
    public Message msg;

	// Append session object to loop
	public abstract AbstractWorkerLoop(ref User user, ref Message msg){
		this.user = user;
        this.msg = msg;
	}

    public abstract string MessageWorker(string msg);
}

// Worker class
public class TestWorkerLoop : AbstractWorkerLoop
{
    public TestWorkerLoop(ref User user, ref Message msg) : base(user, msg){
        this.user = user;
        this.msg = msg;
    }

	public override string MessageWorker(string msg){
		// Do something with client message    
        return "Works";
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontgandrewsView Question on Stackoverflow
Solution 1 - C#KelseyView Answer on Stackoverflow
Solution 2 - C#R. Martinho FernandesView Answer on Stackoverflow
Solution 3 - C#DarioView Answer on Stackoverflow
Solution 4 - C#Charles BretanaView Answer on Stackoverflow
Solution 5 - C#WorksItView Answer on Stackoverflow