Brian Hanf
Brian Hanf

Reputation: 564

Child use of interface

I have a user control that will handle images on a form. But depending on what the source is (web cam or ID scan or other video source) the user control is different.

But they share some common features so I want to create a base class.

My other controls all have some interface items that I need. I would like to declare the interface at the base level though and just implement at the class level. But virtual and override seems to be the closest way to get what I want. Is there any to do it, force the new class to implement the interface assigned at the base class? Looking around it look like making the class abstract (which I don't fully understand) might be a start. If it was just methods that might be alright, but I am also using properties. In that area I have hit a dead end in my searches for answers. Here is what I have so far. Am I on the right track? I just have not worked with abstract classes at all and only limited exposure to interfaces. From the research I think I have the method correct just not sure about the property.

  public interface RequiredAnswer
  {
    void LabelRequiredFieldEmpty();
    bool AnswerRequired{ get;}
  }


  public abstract partial class ExtImage : UserControl, RequiredAnswer
  {
    public virtual bool AnswerRequired
    {
      get
      {
         throw new NotImplementedException ("Answer Required");
      }
    }
    public abstract void LabelRequiredFieldEmpty ()
    {
        //checkBox_AgreementAcceptedText.ForeColor = Color.Red;
    }

So I would have a class

  public partial class ExtImageWebCam : ExtImage
  {
    public override bool AnswerRequired
    {
      get
      {
        return valueFromThisClassThatMeansAnAnswerIsRequired;
      }
    }

     public override void LabelRequiredFieldEmpty ()
     {
       // do something 
     }    
  }

Upvotes: 0

Views: 81

Answers (4)

cadrell0
cadrell0

Reputation: 17327

When you declare a method abstract, you are basically saying that a child class must supply the definition of the method. You can make properties abstract. This sounds like it is exactly what you need.

Here is the MSDN article for further reference.

From MSDN

Properties

Abstract properties behave like abstract methods, except for the differences in declaration and invocation syntax.

It is an error to use the abstract modifier on a static property.

An abstract inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.

Continuing later

In this example, the class DerivedClass is derived from an abstract class BaseClass. The abstract class contains an abstract method, AbstractMethod, and two abstract properties, X and Y.

abstract class BaseClass   // Abstract class
{
    protected int _x = 100;
    protected int _y = 150;
    public abstract void AbstractMethod();   // Abstract method
    public abstract int X    { get; }
    public abstract int Y    { get; }
}

Abstract base class with an Interface

An abstract class must provide implementation for all interface members.

An abstract class that implements an interface might map the interface methods onto abstract methods. For example:

interface I
{
    void M();
}
abstract class C : I
{
    public abstract void M();
}

Upvotes: 2

JG in SD
JG in SD

Reputation: 5607

You are on the right track for the creation of an interface and then defining an abstract class for your purpose.

Standard naming conventions for an interface has been broken however, interfaces are usually prefixed with an I to help identify them

public interface IRequiresAnswer
{
   void LabelRequiredFieldEmpty();
   bool AnswerRequired { get;  }
}

I would also suggest changing the AnswerRequired property to a function as your concrete class says "do somthing to find result". Properties are usually meant to be quick, so performing any calculation within a property is masking that real work takes place when you call the property. With a function it is more apparent to callers that the result will not be achieved immediately.

Upvotes: 0

D Stanley
D Stanley

Reputation: 152624

First of all, interfaces should start with an I by convention, so your interface would be IRequiredAnswer.

Second, if you want to force the inherited classes to implement their own methods rather than inheriting them, just make them abstract in the base class:

public abstract class ExtImage : UserControl, IRequiredAnswer
{
    public abstract bool AnswerRequired  { get; }

    public abstract void LabelRequiredFieldEmpty ();
}

Your child classes would then have to implement the method and property.

Upvotes: 1

BZink
BZink

Reputation: 7967

You're on the right track. Here's a simple example of what you could do. Making the Bar() method abstract forces the inheritors to implement it.

public interface IFoo{
    void Bar();
}

public abstract class BaseFoo : IFoo
{
    public abstract void Bar();

    public void Implemented(){
        Debug.WriteLine("this is a shared implementation");
    }
}

public class KungFoo : BaseFoo{
    public override void Bar()
    {

    }
}

Upvotes: 1

Related Questions