?
Solved

understanding interfaces

Posted on 2014-07-11
3
Medium Priority
?
307 Views
Last Modified: 2014-07-15
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.
0
Comment
Question by:-Dman100-
3 Comments
 
LVL 15

Assisted Solution

by:gplana
gplana earned 668 total points
ID: 40191468
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
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 664 total points
ID: 40191506
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
 
LVL 41

Accepted Solution

by:
Kyle Abrahams earned 668 total points
ID: 40191517
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question