Jump to content
  • Advertisement
Sign in to follow this  
The Orange Peanut

[.net] Difference between abstract class and interface? (which to use?)

This topic is 5470 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Could someone please explain why one of these would be better than the other? I don't know any other way to phrase the question except post the source.
interface AI
{
   int MakeMove(TTTBoard board);
}

class Easy : AI
{
   public int MakeMove(TTTBoard)
   {
      //stuff
   }
};


public abstract class AI
{
   public virtual int MakeMove(TTTBoard board)
   {
      return 0;
   }
};

class Easy : AI
{  
    public override int MakeMove(TTTBoard board)
    {
        //stuff
    }
};


Each would still be instanciated like so... keep in mind that AI would never, under any circumstances, be instanciated.
public class Foo
{
   public static void Main()
   {
        TTTBoard board = new TTTBoard(); 
        AI ai = new Easy();
        ai.MakeMove(board)
   }
};


This isn't my actual source, so if I made a minor mistake please correct me (so I'll know better next time, I havne't tried to compile ^^^that) and excuse me, I apologize :). Which is better, and how do you know which to use? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Abstract classes are very similar to interfaces. There are a few differences that might help you make a choice though:

1. Interfaces only contain method and property declarations and cannot contain any implementation.

2. Abstract classes aren't required to have any abstract members and can also contain implementations for abstract members that may or may not be used by derived classes. Abstract classes also can contain instance fields.

3. A deriving class can only derive from one class, but as many interfaces as it likes.

4. All of the methods and properties in an interface are virtual. This has slight memory and performance penalties when compared with calling non-virtual methods/properties in classes (though all abstract members are virtual, so if all your abstract class's members are virtual/abstract it won't make much difference).

5. Interfaces use different look-up tables for methods and properties than class methods and properties. This may incur a very small performance penalty.

Generally (very generally) an interface defines a standard that many unrelated classes may conform to, while abstract classes are more often used for a specific group of derived classes. There's plenty of exceptions to that rule, if you even want to call it a rule, but if you're having trouble deciding, that might help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!