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
119
votes
2 answers

Why java classes do not inherit annotations from implemented interfaces?

I'm using a Dependency Injection framework (Guice's AOP to intercept some method calls specifically). My class implements an interface and I would like to annotate the interface methods so the framework could select the right methods. Even if the…
Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
119
votes
6 answers

Why are C# interface methods not declared abstract or virtual?

C# methods in interfaces are declared without using the virtual keyword, and overridden in the derived class without using the override keyword. Is there a reason for this? I assume that it is just a language convenience, and obviously the CLR…
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
117
votes
5 answers

Abstract Class vs Interface in C++

Possible Duplicate: How do you declare an interface in C++? This is a general question about C++. As you know, there is no clear distinction between interface and abstract class in C++ unlike Java and C#. When would it be more preferrable to use…
fatma.ekici
  • 2,787
  • 4
  • 28
  • 30
116
votes
6 answers

How to define interfaces in Dart?

In Java, I might have an interface IsSilly and one or more concrete types that implement it: public interface IsSilly { public void makePeopleLaugh(); } public class Clown implements IsSilly { @Override public void makePeopleLaugh() { …
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
115
votes
13 answers

Inner class within Interface

Is it possible to create an inner class within an interface? If it is possible why would we want to create an inner class like that since we are not going to create any interface objects? Do these inner classes help in any development process?
gmhk
  • 15,598
  • 27
  • 89
  • 112
114
votes
3 answers

how to implement Interfaces in C++?

Possible Duplicate: Preferred way to simulate interfaces in C++ I was curious to find out if there are interfaces in C++ because in Java, there is the implementation of the design patterns mostly with decoupling the classes via interfaces. Is…
helpdesk
  • 1,984
  • 6
  • 32
  • 50
113
votes
7 answers

Is it possible to combine members of multiple types in a TypeScript annotation?

It seems that what I am trying to do is not possible, but I really hope it is. Essentially, I have two interfaces, and I want to annotate a single function parameter as the combination of both of them. interface ClientRequest { userId: …
Joshua
  • 2,352
  • 2
  • 19
  • 23
113
votes
5 answers

What's the difference between IComparable & IEquatable interfaces?

Both the interfaces seem to compare objects for equality, so what are the major differences between them?
SoftwareGeek
  • 15,234
  • 19
  • 61
  • 78
112
votes
9 answers

Is it safe for structs to implement interfaces?

I seem to remember reading something about how it is bad for structs to implement interfaces in CLR via C#, but I can't seem to find anything about it. Is it bad? Are there unintended consequences of doing so? public interface Foo { Bar GetBar();…
user1228
111
votes
2 answers

Pros and Cons of Interface constants

PHP interfaces allow the definition of constants in an interface, e.g. interface FooBar { const FOO = 1; const BAR = 2; } echo FooBar::FOO; // 1 Any implementing class will automatically have these constants available, e.g. class MyFooBar…
Gordon
  • 312,688
  • 75
  • 539
  • 559
111
votes
6 answers

How to add a delegate to an interface C#

I need to have some delegates in my class. I'd like to use the interface to "remind" me to set these delegates. How to? My class look like this: public class ClsPictures : myInterface { // Implementing the IProcess interface public event…
Asaf
  • 3,067
  • 11
  • 35
  • 54
111
votes
7 answers

Why an interface can not implement another interface?

What I mean is: interface B {...} interface A extends B {...} // allowed interface A implements B {...} // not allowed I googled it and I found this: implements denotes defining an implementation for the methods of an interface. However…
user467871
110
votes
2 answers

What does an underscore and interface name after keyword var mean?

From http://golang.org/src/pkg/database/sql/driver/types.go: type ValueConverter interface { // ConvertValue converts a value to a driver Value. ConvertValue(v interface{}) (Value, error) } var Bool boolType type boolType struct{} var _…
dilfish
  • 1,329
  • 2
  • 11
  • 11
109
votes
3 answers

Why static classes cant implement interfaces?

Possible Duplicate: Why Doesn’t C# Allow Static Methods to Implement an Interface? In my application I want to use a Repository that will do the raw data access (TestRepository, SqlRepository, FlatFileRepository etc). Because such a repository…
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
108
votes
15 answers

Abstract class vs Interface in Java

I was asked a question, I wanted to get my answer reviewed here. Q: In which scenario it is more appropriate to extend an abstract class rather than implementing the interface(s)? A: If we are using template method design pattern. Am I correct? I am…
lowLatency
  • 5,534
  • 12
  • 44
  • 70