0

I'm a bit new to OO programming and I'm trying to understand all facets of this kind of practice : inheritance, polymorphism and such, but there's a thing my brain DOESN'T WANT to fully understand: Interfaces.

I can understand the benefits of using interfacing instead of class-inheritance (mostly because a class can't inherit from multiple parents) but here's where I'm stuck:

Let's say I have something like this:

/** a bunch of interfaces **/
public interface IMoveable
{
    void MoveMethod();
}

public interface IKilleable()
{
    void KillMethod();
}

public interface IRenderable()
{
    void RenderMethod();
}

/** and the classes that implement them **/
public class ClassOne : IMoveable
{
    public void MoveMethod() { ... }
}

public class ClassTwo: IMoveable, IKilleable
{
    public void MoveMethod() { ... }
    public void KillMethod() { ... }
}

public class ClassThree: IMoveable, IRenderable
{
    public void MoveMethod() { ... }
    public void RenderMethod() { ... }
}

public class ClassFour: IMoveable, IKilleable, IRenderable
{
    public void MoveMethod() { ... }
    public void KillMethod() { ... }
    public void RenderMethod() { ... }
}

By using interfaces here, I would have to declare MoveMethod, KillMethod and RenderMethod each time, in each classes... That means duplicating my code. There must be something wrong, because I don't find this really practical.

So should I implement interfaces only on a few classes? Or should I find a way to mix inheritance and interfaces?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Chupon
  • 3
  • 3
  • Possibly best path to understand is to write unit test that verifies that dialog shown before formatting your harddrive only when you pass flag to `FormatHardDrive(bool really)`. Implement such method (does not have to actually destroy harddrive, but something like it) and try to write test. – Alexei Levenkov Nov 18 '14 at 03:56
  • [here](http://stackoverflow.com/questions/6802573/c-sharp-interfaces-whats-the-point) is a good example I liked – Jonesopolis Nov 18 '14 at 04:00

1 Answers1

2

Interfaces are like a contract to a class.. If some class states that it supports such an interface, it must have it's method defined as you properly sampled. Interfaces are great to expose common things that don't easily cross different class implementations.

Now, from your samples, you may be best to do a combination to prevent duplicate code by subclassing from a class and ALSO an interface. So you can get parent-structure code constant and expand as needed.

/** Based on same interfaces originally provided... and the classes that implement them **/
public class ClassOne : IMoveable
{
    public void MoveMethod() { ... }
}

public class ClassTwo: ClassOne, IKilleable
{
    // Move Method is inherited from ClassOne, THEN you have added IKilleable
    public void KillMethod() { ... }
}

public class ClassThree: ClassOne, IRenderable
{
    // Similar inherits the MoveMethod, but adds renderable
    public void RenderMethod() { ... }
}

public class ClassFour: ClassTwo, IRenderable
{
    // Retains inheritance of Move/Kill, but need to add renderable
    public void RenderMethod() { ... }
}
DRapp
  • 47,638
  • 12
  • 72
  • 142