1

I am new to Java/Android I have some c++/c# OOP background and from I'm really just trying to understand interfaces a little better. From what I've gathered, they basically act like prototypes that you would declare in c++. Is this at all correct? Or is there a better way of understanding this?

Joel
  • 4,631
  • 9
  • 33
  • 51
  • Doesn't C# have interfaces too? – JB Nizet Feb 13 '14 at 22:43
  • yeah have not learned them yet though. (still a student). –  Feb 13 '14 at 22:44
  • If you really understand what's a [prototype](http://en.wikipedia.org/wiki/Function_prototype) and what's an [interface](http://en.wikipedia.org/wiki/Interface_(object-oriented_programming)), then you might know that thery **are not** the same. – Luiggi Mendoza Feb 13 '14 at 23:01
  • There's lots of good (answered) questions already about exactly this. This one (http://stackoverflow.com/questions/11945993/equivalent-of-java-interfaces-in-c) has some good answers, and references to other questions of the same type – Gus Feb 13 '14 at 23:03
  • No, they're not even similar. – Luiggi Mendoza Feb 13 '14 at 23:04
  • What much people forget when answering this is that a class can implement more than one interface, and two or several interfaces can have this method with same name but different arguments or even different exceptions to throw and the class that implements these interfaces must accomplish implementing all the interfaces and their methods. – Luiggi Mendoza Feb 13 '14 at 23:07
  • Belongs on programmer.stackexchange.com – mydoghasworms Feb 14 '14 at 06:56

3 Answers3

0

Think of it as a class with only pure virtual methods.

In C++:

class ISomething {
    public:
        virtual void f() = 0;
};

In Java/C#:

public interface ISomething {
    /* public */ void f(); // everything is virtual in Java
}
TWiStErRob
  • 38,918
  • 20
  • 146
  • 220
  • would your C++ code have a ; after the last '}' (since that would be in a header file). –  Feb 13 '14 at 22:59
  • Sorry, my C++ is a little rusty (e.g. I didn't know about the extension __interface), I was thinking along these lines: http://stackoverflow.com/questions/318064/how-do-you-declare-an-interface-in-c – TWiStErRob Feb 13 '14 at 23:03
0

Interfaces are contracts you specify to help communicate with you "clients". Clients are other classes who need to use your classes without really knowing how they work internally.

You then create different implementations (or only one if you want) who will have to absolutely respect that contract everyone else agreed upon.

As a developper in a small team, it's really helpful to first define all our interfaces, and then use them without the classes being finished.

So yes, they are a lot like prototypes in C, at least that's why I used prototypes myself.

Pierre
  • 784
  • 8
  • 21
0

The interface is concept that represent a contract that every class can implement. The concept is not related to any particular language that allow to code in Object Oriented parading.

C++

__interface IMyInterface
{
    virtual HRESULT M1() = 0;   // pure virtual
    HRESULT M2();           // also pure virtual
}

Or you can use the struct as well

C#

   interface IMyInterface {
       Hresult M1();
       Hresult M2();
   }

Java

   interface IMyInterface {
       Hresult M1();
       Hresult M2();
   }

In Java language to implement a interface you must enumerate it after key word implements and all method to its body if class is not marked as abstract. In addition from version 1.6 a good practice is to use annotation @Override (about) that inform the compiler that annotated method is part of some abstract element.

  class MyClass implements IMyInterface {

        @Override
        public Hresult M1() {
           return null;
        }

        @Override
        Hresult M2() {
            return null;
        }

  }

The Oracle tutorial on interface

Finally to answer your question, does the prototype from C are the same as interfaces in Java. We can say that they can be used to gain some abstract level in code, on on that level are the same.