Luke
Luke

Reputation: 544

Overriding Base Class abstract methods and hiding them in derived classes

I have two two types of base class;

public abstract class Base
{
    public abstract object Work();
}

public abstract class AuthenticatedBase : Base
{
    public abstract object Work(T request);
}

The authenticated class does some extra work to check a login beforehand. Then I have different classes extending from both base classes;

public class A : Base
{
    public override object Work()
    {
        // Work here
    }
}

public class B : AuthenticatedBase
{
    public override object Work(T request)
    {
        // Work here
    }
}

Basically, when I create a new class B that derives from AuthenticatedBase, Visual Studio says I need to implement Work() from the Base class even though I have an alternate implementation in AuthenticatedBase that I am overriding, admittedly with different parameters. What should I be doing so that I don't have to implement the Work() method from the base class in inherited classes?

Upvotes: 0

Views: 904

Answers (2)

Brannon
Brannon

Reputation: 5424

Implement the parameterless Work method in your AuthenticatedBase and use the "sealed" keyword to stop it from showing on inheritors of AuthenticatedBase.

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 190996

You have to implement it, there is no way around it. This is the case where multiple inheritance would come in handy.

You could use composition so that B has a reference to an A. Then B.Work() can call A.Work().

Alternatively, implement B.Work() so that it calls B.Work(T). Or even have Base.Work() be a virtual method with the base implementation from A.

Upvotes: 0

Related Questions