understanding interfaces

I'm trying to get a better handle on using interfaces.  I have used interfaces that other developers have created, but I have not created an interface.  I understand that they are a collection of methods and also that interfaces can be extended to include other methods.

When you implement an interface, does the order in which you call the methods matter?  For example, as long as I call each method in the interface, does it matter if I call the methods in the same order as defined in the interface or can I call them in any order as long as all the methods are called?

All the interfaces I've worked with contain either void methods with no method  body or methods that return simple types (strings, integers, etc.) with just the return statement.  Can you use methods that return more complex types?  Can the methods contain logic in the method body?

I'm trying to understand the how, when and why of creating an interface in a real-world scenario.  So, if I'm coding a solution, I'd have a better ability to identify when creating an interface would make sense.

Any help on how to better understand using interfaces would be great!
Thanks in advance.
-Dman100-Software ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gplanaCommented:
Interfaces are just a set of definition of methods. It doesn't matter in which order do you call them.

A java interface is just as a java class, but without the code of the methods.

Please see this link for more information: http://en.wikipedia.org/wiki/Interface_%28Java%29

Hope it helps. Regards.
0
käµfm³d 👽Commented:
Let's clarify a few things first:

You didn't specify a language that you are using. Many languages define the term "interface", and most often it means the same thing. It may be worth mentioning what programming language you are working with.

An interface in (C# and Java) defines a list of methods (or sometimes other things, again depending on the language) that are required to be present on the class which implements the interface. In this way, you are defining a contract that your class must meet in order to be valid if it will implement an interface.

Because an interface simply defines what methods should be available, there is no method body defined within the interface definition. Therefore, you don't "call" an interface method. That said, I'm wondering if you meant "declare" instead "call"?
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Just to answer a few of the other questions;

An interface (at least in .Net) can return more complex types.
There is no code in a method body.

From http://msdn.microsoft.com/en-us/library/3b5b8ezk(v=vs.90).aspx
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.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Theory

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.