Questions tagged [interface]

An interface refers to the designated point of interaction with a component. Interfaces are applicable at both the hardware and software level. --- It also refers to the language-element `interface`, which is the sole exception to single-inheritance in Java, C# and similar languages.

An interface is the designated point of interaction with a component.

Well-defined, comprehensive, versatile but still minimal interfaces are critical for reliability, extensibility, maintainance, and compatibility.

In programming, we mostly encounter:

  • s, which consist of symbols (types (and all they might entail), functions, constants, variables, templates, namespaces and so on), and their defined behavior, which might include their own turing-complete languages.
  • s, which define the mapping of APIs to the underlying machine.
  • s, which define interactions over a connection, network, or the like.
  • And many combined ones, especially in directly interacing with a device, for example to write a device-driver.

In some languages, mostly object-oriented single-inheritance ones, an interface is a restricted type not containing any state nor instantiable by itself, and only able to inherit from (any number of) other interfaces.
A can implement any number of interfaces in addition to inheriting from a single other class (most of those languages enforce a single-root inheritance-hierarchy).
Most insist that the base-class is listed first, and some allow explicitly implementing an interfaces members, which makes them inaccessible except through an interface-pointer of that type.

The implementation of an interface varies between programming languages and environments; for e.g. C# has the following definition (Interfaces (C# Programming Guide));

Interfaces describe a group of related functionalities that can belong to any class or struct. You define an interface by using the interface keyword, as shown in the following example.

interface IEquatable<T> {
  bool Equals(T obj);
}

An interface may or may not contain or make use of any number of interfaces or their equivalent.

You can "program to the interface, not the implementation" without using any interface, as well as fail to but still use interfaces all over the place.

The [interface]-tag should should be used in conjunction with the appropriate [language]-tag(s) where applicable.

19932 questions
92
votes
10 answers

Attributes / member variables in interfaces?

I wish to know is there any way in which I can make it compulsory for the implementer class to declare the objects handles/primitives as they do with methods. for e.g.: public interface Rectangle { int height = 0; int width = 0; …
Shrey
  • 2,374
  • 3
  • 21
  • 24
92
votes
7 answers

Java - Method name collision in interface implementation

If I have two interfaces , both quite different in their purposes , but with same method signature , how do I make a class implement both without being forced to write a single method that serves for the both the interfaces and writing some…
Bhaskar
  • 7,443
  • 5
  • 39
  • 51
92
votes
18 answers

When do I have to use interfaces instead of abstract classes?

I was wondering when I should use interfaces. Lets think about the following: public abstract class Vehicle { abstract float getSpeed(); } and : public interface IVehicle { float getSpeed(); } I can easily implement both of them, they have…
augmentie123
  • 1,019
  • 1
  • 8
  • 10
92
votes
1 answer

Typescript Function Interface

Why doesn't Typescript warn me that the function I am defining does not match the interface declaration, but it does warn me if I try to invoke the function. interface IFormatter { (data: string, toUpper : boolean): string; }; //Compiler does…
MBeckius
  • 1,447
  • 2
  • 12
  • 14
92
votes
15 answers

Why no static methods in Interfaces, but static fields and inner classes OK? [pre-Java8]

There have been a few questions asked here about why you can't define static methods within interfaces, but none of them address a basic inconsistency: why can you define static fields and static inner types within an interface, but not static…
skaffman
  • 398,947
  • 96
  • 818
  • 769
91
votes
7 answers

How to find which classes implement a particular interface in Eclipse?

I have an application as JAR file with many dependencies. For some reason I need to decompile one of the libraries and open it up with Eclipse. For a given interface in the project, is there a way to find the class(s) that implements it? It may be…
Am1rr3zA
  • 7,115
  • 18
  • 83
  • 125
91
votes
4 answers

Can an interface method have a body?

I know that an interface is like a 100% pure abstract class. So, it can't have method implementation in it. But, I saw a strange code. Can anyone explain it? Code Snippet: interface Whoa { public static void doStuff() { …
user3034861
90
votes
2 answers

The 'instanceof' operator behaves differently for interfaces and classes

I would like to know regarding following behavior of instanceof operator in Java. interface C {} class B {} public class A { public static void main(String args[]) { B obj = new B(); System.out.println(obj instanceof A); …
Ajay Sharma
  • 846
  • 10
  • 21
89
votes
6 answers

How to write junit tests for interfaces?

What is the best way to write junit tests for interfaces so they can be used for the concrete implementing classes? e.g. You have this interface and implementing classes: public interface MyInterface { /** Return the given value. */ public…
Xeno Lupus
  • 1,504
  • 3
  • 12
  • 17
89
votes
7 answers

Is it possible to have an interface that has private / protected methods?

Is it possible in PHP 5 to have an interface that has private / protected methods? Right now I have: interface iService { private method1(); } That throws an error: Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE I just…
teepusink
  • 27,444
  • 37
  • 107
  • 147
88
votes
5 answers

Fields in interfaces

I have a basic question in Java, but it's a general question in OOP. Why do interfaces allow fields to be set? Doesn't that run contrary to what an interface is supposed to do? The way I made sense of it, an interface is what in English would be an…
Sal
  • 3,179
  • 7
  • 31
  • 41
88
votes
8 answers

How to Implement IComparable interface?

I am populating an array with instances of a class: BankAccount[] a; . . . a = new BankAccount[] { new BankAccount("George Smith", 500m), new BankAccount("Sid Zimmerman", 300m) }; Once I populate this array, I would like to sort it by…
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
88
votes
7 answers

Why are interfaces needed in Golang?

In Golang, we use structs with receiver methods. everything is perfect up to here. I'm not sure what interfaces are, however. We define methods in structs and if we want to implement a method on a struct, we write it anyway again under another…
nikoss
  • 3,254
  • 2
  • 26
  • 40
87
votes
15 answers

Why can't I have protected interface members?

What is the argument against declaring protected-access members on interfaces? This, for example, is invalid: public interface IOrange { public OrangePeel Peel { get; } protected OrangePips Seeds { get; } } In this example, the interface…
ajlane
  • 1,899
  • 1
  • 18
  • 23
87
votes
1 answer

MOQ - how to mock an interface that needs to be cast to another interface?

what I want to do is construct a moq for I1 - which is fine ... however in the course of the method that I am testing that uses this mock I need to cast it to I2 in order to access some properties that are not on I1 Interface I1 { int…
John Nicholas
  • 4,778
  • 4
  • 31
  • 50