Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Interfaces vs Abstract classes why even use contracts?

$
0
0

Okay, I know there are lots of posts on this. I've read several and they have all helped me get to a reasonable understanding of the differences between both; however, I still can not seem to comprehendwhy anyone would need to enforce a contract for sub classes to follow?

Correct me if I am wrong abstract classes and interfaces are both basically the same thing with the main difference being a subclass can inherit multiple interfaces while it would not be able to implement more than one abstract class. 

So then this is where I get confused. I know that abstract classes do allow implementation of methods declared virtual which can be overridden in subclasses. This seems like a very useful technique and I can understand why one would use an abstract class for this purpose. 

Now I don't understand why someone would use an abstract class or interface with no type of implementation. Why not just cut those out and define your classes?

//Not written in VS. Might have typos

public interface ICanine
{
    public void Bark();

}

public class Beagle : ICanine
{
    public void Bark()
    {
      //Bark
    }
}


public class Wolf : ICanine
{
    public void Bark()
    {
      //Bark
    }

}

//Why not omit the interface and just do this instead?

public abstract class Canine
{
   public virtual void Bark()
   {
      //Perform Function A
   }
}

public class Beagle
{
    //Base class Bark Suffices
}


public class Wolf
{
    public override void Bark()
    {
      //Perform function B
    }

}

//I just don't see the point in implementing something that has no functionality. 

 

Viewing all articles
Browse latest Browse all 18858

Trending Articles