0

Possible Duplicate:
Interface or abstract class?

I have a group of classes defined as follows:

namespace VGADevices.UsingAbstractClass
{
    public abstract class VGA
    {
        public abstract int HorizontalResolution { get; set; }
        public abstract int VerticalResolution { get; set; }
    }
    public class LCDScreen : VGA
    {
        public override int HorizontalResolution { get; set; }
        public override int VerticalResolution { get; set; }
    }
}  // namespace VGADevices.UsingAbstractClass

namespace VGADevices.UsingInterfaces
{
    public interface IVGA
    {
        int HorizontalResolution { get; set; }
        int VerticalResolution { get; set; }
    }
    public class LCDScreen : IVGA
    {
        public virtual int HorizontalResolution { get; set; }
        public virtual int VerticalResolution { get; set; }
    }
}  // namespace VGADevices.UsingInterfaces

Client code, I have the choice between:

class Computer
{
        public VGA VGAOutput { get; set; }
}

or

class Computer
{
        public IVGA VGAOutput { get; set; }
}

I read somewhere that using interfaces is better, but why? With abstract classes I can define an interface as well plus add data-members so why are interfaces the preferred method? Does binary replacement play a role here as well?

thank you

Chris

Community
  • 1
  • 1
ChrisPeeters
  • 153
  • 2
  • 13

2 Answers2

2

You can inherit from(that is, implement) multiple interfaces. You can't inherit from multiple abstract classes

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
0

Check out this post:

http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx

Most important features I see: Multiple inheritance (interface can implement multiple interfaces, abstract can inherit from only one) Homogeneity (I have two objects with the same interface, but they're really not they same object and shouldn't share any code)

DanTheMan
  • 3,277
  • 2
  • 21
  • 40