4

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

Levi Botelho
  • 24,626
  • 5
  • 61
  • 96
Kooki
  • 1,157
  • 9
  • 30
  • Why did you unaccept my answer? Did you find better information? If so please post and accept your own response so that the question is marked as answered. – Levi Botelho Dec 17 '12 at 15:00

6 Answers6

4

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.

Levi Botelho
  • 24,626
  • 5
  • 61
  • 96
3

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();
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • But I need a implemantation in baseclass too. But if there is no other poosibility I would implement a new Method called "BaseRefresh", and a childclass can call it on interfaceimplementation – Kooki Dec 11 '12 at 10:21
  • @Kooki - Why do you need an implementation in `BaseClass` too? – Oded Dec 11 '12 at 10:21
  • Cause there are some fields, which have to set into BaseClass with these "Refresh" – Kooki Dec 11 '12 at 10:26
  • The code above is an exact replication of IBaseClass, though. You could do this, but why would you? – PeteH Dec 11 '12 at 10:30
  • exactly, abstract is the solution, interface reimplementation in derived class can be subtle : http://blogs.msdn.com/b/ericlippert/archive/2011/12/08/so-many-interfaces-part-two.aspx – AlexH Dec 11 '12 at 10:33
  • @PeteH - That's the question I had, the comment by the OP suggests that the base class is needed for other shared data. – Oded Dec 11 '12 at 10:38
3

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();
}
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
1

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.

Mert Akcakaya
  • 3,109
  • 2
  • 31
  • 42
1

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.
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
0

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();
        }
    }
}
ROOMY
  • 53
  • 8
  • If `Base1` explicitly expects child classes to be able to implement their own version of `Refresh`, `Base1` should define the method as `virtual`. – comecme Dec 11 '12 at 14:41
  • Not an ideal solution as the new keyword implies method hiding. The new keyword should be used to define new method / property implementations where the original is not virtual (cannot override). When you are designing classes and you know that the method will be overridden, then make the method virtual. – Matthew Layton Dec 11 '12 at 15:07