Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Interfaces and Classes


Matt
 Share

Recommended Posts

I have no idea what the purpose or advantage is of using an interface. Methods declared in an interface have to be defined in the class that inherits from it anyways, so why use the interface at all?

To me, this

```

public interface IRandom
{
void Method();
}

public class RandomClass : IRandom
{
public void Method()
{
Console.WriteLine("Hello World.");
}
}

```
is no different than

```

public class RandomClass
{
public void Method()
{
Console.WriteLine("Hello World.");
}
}

```
Link to comment
Share on other sites

There is no difference in what you put. The interface only makes sure the class that you attach the interface to has all the methods included in the class. For example, if you have a plugin system for your engine, if someone were to make their own plugin class, they can use the IPlugin interface you created to ensure their plugin class contains all the methods required to complete the plugin. The application will error if you dont have the the methods inside the interface in the class that calling the interface.
Link to comment
Share on other sites

That makes more sense, but why opt for the error method? Why not do something like this?

```

public class InterfaceAlternative
{
public virtual void BasicMethod()
{
Console.WriteLine("Method not implemented.");
}
}

public class TestClass : InterfaceAlternative
{

}

```
If there isn't an overriding method in the TestClass, the base method is ran. There you could have some sort of system that notifies the user that he/she is missing a necessary method and the application runs.
Link to comment
Share on other sites

Which ever way works for you, I suppose. However, the main reason for interfaces is they allow you to implement methods that a class would need in order to run properly. The benefit of having an interface is that C# doesn't support multiple class inheritance. You can, however, attach multiple interfaces to a class.
Link to comment
Share on other sites

The purpose of an interface is not for just a single project but more for making it so you and others have to use specific funtions for that class to "interface" or interact with the rest of the program.

like in java when you make a new thread you have to use the Runnable interface because the Thread class NEEDS a run method specifically made from that interface. It;s a way of controlling how other interact with what you already made the way you want them to interact with it.

a good common use for interfaces is to have them as the thing you implement into plugins so it generates the necessary methods for interacting with the rest of the game/program the way the original developer intended.

at first i didn't quite get why one would want to use interfaces either, then I started looking how existing APIs and kits use them and it made more sense for quality control for when you pass an API or kit on to the next developer.

In game related I might have the interface "Useable" and implement it in the classes of  Items, InGameObject, and Doors. That would generate a "use" method in each of those classes that a different class would call for each instance of them, but they would each have their own set of operations different from each other.

From the MS Docs:

 Interfaces are a powerful programming tool because they let you separate the definition of objects from their implementation. Interfaces and class inheritance each have advantages and disadvantages, and you may end up using a combination of both in your projects.

There are several other reasons why you might want to use interfaces instead of class inheritance:
-Interfaces are better suited to situations in which your applications require many possibly unrelated object types to provide certain functionality.
-Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.
-Interfaces are better in situations in which you do not have to inherit implementation from a base class.
-Interfaces are useful when you cannot use class inheritance. For example, structures cannot inherit from classes, but they can implement interfaces.
Link to comment
Share on other sites

Not sure if this has been mentioned but you get abstractness (if that's even a word) from interfaces. A good example in a game would be an interface called item (Let it be IItem).

This IItem is basically our baseless item. It doesn't have any extra information other than what all it should do. I can now create an specific item class like Gold which is based upon IItem meaning that I get everything IItem has. 

An example of using IItem would be like this:

Say you have a method like UseItem which accepts an item. If you didn't use an interface you would have to declare a UseItem for every type of item or create a method with a parameter type of a variant/object. If you used an interface the variable type could be IItem and boom you can pass in a class as long as it based upon IItem. 

[Outside Link](http://programmers.stackexchange.com/questions/145437/why-use-an-interface-when-the-class-can-directly-implement-the-functions)
Link to comment
Share on other sites

  • 1 month later...
Part of the difference has been mentioned (most of it has but its been fairly verbose). Multiple class inheritance doesn't exist in C# whereas multiple interface inheritance does.

primarily, interfaces are used to bind classes to a "data contract" (buzzword).
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...