views:

117

answers:

4

Hi

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

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 .NET2 app.

Thanks in advance.

+10  A: 

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
    }
}
Kelsey
See the comment I left against Charles's response
tgandrews
Wow thanks that worked. I didn't realise abstract classes could have methods with functionality in them.
tgandrews
Abstract methods do not have functionality in them; the functionality needs to be in the inherited classes
Andreas Grech
But they can have other non-abstract methods (and virtual methods, overridden methods, anything a normal class can have)
thecoop
+3  A: 

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.

Charles Bretana
The compiler is complaining at me that the base class must be abstract for this to work. Maybe this is because it is .NET 2?
tgandrews
@Thoku: that happens in all versions of the language. You can't have concrete classes with abstract methods.
Martinho Fernandes
+1  A: 

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.

Dario
Yes, I didn't realise you could do this. I'm loving C# more and more with every day.
tgandrews
@Thoku: this is not C#-specific behavior. All class-based object-oriented languages exhibit this behavior.
Martinho Fernandes
+2  A: 

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");
    }
}
Martinho Fernandes