Question

I did not fully understand using Interfaces, so I have to ask :-)

I use a BaseClass, which implements the IBaseClass interface.These interface only contains one declaration :

public interface IBaseClass
{
    void Refresh ();
}

So I have implement a Refresh method in my Baseclass :

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

Now I want to use some classes which extends from these Baseclass and implements the IBaseClass interface :

    public class ChildClass : BaseClass,IBaseClass
    {
    }

But cause of the implementation of "Refresh" into my BaseClass I does not have to implement the method again. What should I do, to force the implementation of "Refresh" into all childs of BaseClass as well as all childclasses of childclass.

Thanks kooki

Was it helpful?

Solution

You cannot force derived classes to re-implement the method in the way that you have specified. You have three options:

  1. Do not define refresh in the base class. The interface will force child classes to implement it.
  2. Eschew the interface if its only purpose was to force implementation and declare the base class as abstract as well as refresh, for which you would not give an implementation.
  3. Define refresh in the base class as virtual. This allows overrides but will not force them. This is how ToString() works.

This is all assuming that your base class is larger than a single method. If indeed your code is exactly what you posted then Oded's answer is the best choice.

OTHER TIPS

Simple. Don't supply a base class implementation, and you will have to implement the method in every inheriting class.

One way to achieve that is to make BaseClass abstract:

public abstract BaseClass : IBaseClass
{
    public abstract void Refresh();
}

What should I do, to force the implementation of "Refresh" into all childs of BaseClass as well as all childclasses of childclass.

Like this:

interface IBase
{
    void Refresh();
}

abstract class BaseClass : IBase
{
    public abstract void Refresh();
}

class ChildClass : BaseClass
{
    public override void Refresh()
    {
        // Your code
    }
}

You can even omit the interface (my rule of thumb: if an interface gets implemented by exactly one class, dump the interface. Don't cling on to interfacitis. An abstract class quite much represents an interface, see also Interface vs Abstract Class (general OO)).

If you do need an implementation in the base class, build it as such:

(abstract) class BaseClass ( : IBase)
{
    public virtual void Refresh()
    {
        // Your code
    }
}

Which you can then call from your derived classes:

public override void Refresh()
{
    // Your code

    // optionally, to call the base implementation:
    base.Refresh();
}

If you want to supply a default implementation, do it in your base class by marking it as virtual, so you can override that implementation in subclasses if you want.

Otherwise mark the method as abstract in your base class, so your subclasses are forced to implement the method themselves.

Lets take a look at this step-by-step.

1: You have an interface which defines your code contract defined like so:

public interface IBase
{
    void Refresh();
}

2: You have a base class which implements your interface. (you will notice that the implementation for refresh is virtual. This allows you to override this method in derived classes).

class Base : IBase
{
    public virtual void Refresh()
    {
        //Implementation
    }
}

3: You have a super class which derives from Base. (you will notice that the derived class does not need to explicitly implement IBase as it's done at a lower level. I'll show you that you can test the integrity of this).

class Child : Base
{
    public override void Refresh()
    {
        base.Refresh(); //You can call this here if you need to perform the super objects Refresh() before yor own.
        //Add your implementation here.
    }
}

At this point you might be thinking; "Ok, well then how is Child implementing IBase?". The answer is that it is implemented indirectly through Base, and because Child inherits Base, it also gets the implementation for IBase.

Therefore if you were to write:

IBase instance = new Child();

This is perfectly legal because essentially, Child derives from IBase indirectly.

If you wanted to test this, you can do this in your code:

bool canAssign = typeof(IBase).IsAssignableFrom(typeof(Child));
//canAssign should be true as Child can be assigned from IBase.

May be New Keyword can help u in that;

namespace ConsoleApplication1
{
    interface IBase
    {
        void Referesh();
    }
    public class Base1 : IBase
    {
        public void Referesh()
        {
            Console.WriteLine("Hi"); 
        }
    }
    public class Class1 : Base1, IBase
    {
        public new void Referesh()
        {
            Console.WriteLine("Bye");
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Class1 obj = new Class1();
            obj.Referesh();

            Console.ReadKey();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top