understanding interfaces

Posted on 2014-07-11
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.
Question by:-Dman100-
    LVL 15

    Assisted Solution

    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:

    Hope it helps. Regards.
    LVL 74

    Assisted Solution

    by:käµfm³d 👽
    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"?
    LVL 39

    Accepted Solution

    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.

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now