Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force sub classes to implement a method

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.

like image 873
tgandrews Avatar asked Nov 20 '09 16:11

tgandrews


People also ask

Do subclasses have to implement all abstract methods?

The subclass of abstract class in java must implement all the abstract methods unless the subclass is also an abstract class. All the methods in an interface are implicitly abstract unless the interface methods are static or default.

Does abstract class force implementation?

Note that making an abstract method in an abstract class does not require subclasses to implement the method. You can make as many subclasses that do not implement it as you like; they just all have to be abstract too.

How do you force a method to be overridden?

Just make the method abstract. This will force all subclasses to implement it, even if it is implemented in a super class of the abstract class. Show activity on this post.


3 Answers

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
    }
}
like image 174
Kelsey Avatar answered Oct 19 '22 06:10

Kelsey


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.

like image 33
Dario Avatar answered Oct 19 '22 06:10

Dario


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");
    }
}
like image 42
R. Martinho Fernandes Avatar answered Oct 19 '22 06:10

R. Martinho Fernandes