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
107
votes
6 answers

How do arrays in C# partially implement IList?

So as you may know, arrays in C# implement IList, among other interfaces. Somehow though, they do this without publicly implementing the Count property of IList! Arrays have only a Length property. Is this a blatant example of C#/.NET…
MgSam
  • 12,139
  • 19
  • 64
  • 95
106
votes
7 answers

What's the difference between HashSet and Set?

Saw the code snippet like Set instances = new HashSet(); I am wondering if Hashset is a special kind of set. Any difference between them?
user496949
  • 83,087
  • 147
  • 309
  • 426
106
votes
11 answers

How can I get a list of all the implementations of an interface programmatically in Java?

Can I do it with reflection or something like that?
user2427
  • 7,842
  • 19
  • 61
  • 71
106
votes
10 answers

What is the purpose of a marker interface?

What is the purpose of a marker interface?
coder
  • 3,205
  • 5
  • 26
  • 14
105
votes
7 answers

Why are all fields in an interface implicitly static and final?

I am just trying to understand why all fields defined in an Interface are implicitly static and final. The idea of keeping fields static makes sense to me as you can't have objects of an interface but why they are final (implicitly)? Any one knows…
peakit
  • 28,597
  • 27
  • 63
  • 80
104
votes
3 answers

What is the "default" implementation of method defined in an Interface?

In the Collection Interface I found a method named removeIf() that contains its implementation. default boolean removeIf(Predicate filter) { Objects.requireNonNull(filter); boolean removed = false; final Iterator
gifpif
  • 4,507
  • 4
  • 31
  • 45
99
votes
4 answers

PHP 7 interfaces, return type hinting and self

UPDATE: PHP 7.4 now does support covariance and contravariance which addresses the major issue raised in this question. I have run into something of an issue with using return type hinting in PHP 7. My understanding is that hinting : self means…
GordonM
  • 31,179
  • 15
  • 87
  • 129
98
votes
7 answers

When to use mixins and when to use interfaces in Dart?

I'm very familiar with the concepts of interfaces and abstract classes, but not super familiar with the concepts of mixins. Right now, in Dart, every class A defines an implicit interface, which can be implemented by another class B by using the…
nbro
  • 15,395
  • 32
  • 113
  • 196
97
votes
7 answers

Abstract classes vs. interfaces vs. mixins

Could someone please explain to me the differences between abstract classes, interfaces, and mixins? I've used each before in my code but I don't know the technical differences.
Sasha Chedygov
  • 127,549
  • 26
  • 102
  • 115
96
votes
4 answers

When is an interface with a default method initialized?

While searching through the Java Language Specification to answer this question, I learned that Before a class is initialized, its direct superclass must be initialized, but interfaces implemented by the class are not initialized. Similarly, the…
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
96
votes
14 answers

Should interfaces be placed in a separate package?

I'm new to a team working on a rather large project, with lots of components and dependencies. For every component, there's an interfaces package where the exposed interfaces for that component are placed. Is this a good practice? My usual practice…
kctang
  • 10,894
  • 8
  • 44
  • 63
95
votes
6 answers

C# - Cannot implicitly convert type List to List

I have a project with all my Interface definitions: RivWorks.Interfaces I have a project where I define concrete implmentations: RivWorks.DTO I've done this hundreds of times before but for some reason I am getting this error now: Cannot…
Keith Barrows
  • 24,802
  • 26
  • 88
  • 134
94
votes
2 answers

Understanding Covariant and Contravariant interfaces in C#

I've come across these in a textbook I am reading on C#, but I am having difficulty understanding them, probably due to lack of context. Is there a good concise explanation of what they are and what they are useful for out there? Edit for…
NibblyPig
  • 51,118
  • 72
  • 200
  • 356
94
votes
4 answers

Why PHP Trait can't implement interfaces?

I'm wondering why PHP Trait (PHP 5.4) cannot implement interfaces. Update from user1460043's answer => ...cannot require class which uses it to implement a specific interface I understand that it could be obvious, because people could think that if…
Leto
  • 2,594
  • 3
  • 24
  • 37
93
votes
14 answers

A difference in style: IDictionary vs Dictionary

I have a friend who's just getting into .NET development after developing in Java for ages and, after looking at some of his code I notice that he's doing the following quite often: IDictionary dictionary = new Dictionary
rein
  • 32,967
  • 23
  • 82
  • 106