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
195
votes
7 answers

IntelliJ IDEA jump from interface to implementing class in Java

Is there some shortcut that would allow me after creating method in an interface, select and jump to implementing class of that interface?
MatBanik
  • 26,356
  • 39
  • 116
  • 178
192
votes
21 answers

Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?

Java doesn't allow multiple inheritance, but it allows implementing multiple interfaces. Why?
abson
  • 9,148
  • 17
  • 50
  • 69
189
votes
6 answers

Checking if an instance's class implements an interface?

Given a class instance, is it possible to determine if it implements a particular interface? As far as I know, there isn't a built-in function to do this directly. What options do I have (if any)?
Wilco
  • 32,754
  • 49
  • 128
  • 160
188
votes
7 answers

What does "program to interfaces, not implementations" mean?

One stumbles upon this phrase when reading about design patterns. But I don't understand it, could someone explain this for me?
never_had_a_name
  • 90,630
  • 105
  • 267
  • 383
183
votes
4 answers

When use a interface or class in Typescript

I have a simple scenario of a login that requires user's input a email and password in Typescript. I need to create some type to get this strong-typed and send it to the back-end. Should this be written as: export interface UserLogin { email:…
Josbel Luna
  • 2,574
  • 3
  • 17
  • 25
179
votes
9 answers

How to make a Java class that implements one interface with two generic types?

I have a generic interface public interface Consumer { public void consume(E e); } I have a class that consumes two types of objects, so I would like to do something like: public class TwoTypesConsumer implements Consumer,…
daphshez
  • 9,272
  • 11
  • 47
  • 65
174
votes
18 answers

Custom fonts and XML layouts (Android)

I'm trying to define a GUI layout using XML files in Android. As far as I can find out, there is no way to specify that your widgets should use a custom font (e.g. one you've placed in assets/font/) in XML files and you can only use the system…
DrDefrost
  • 1,807
  • 3
  • 12
  • 5
173
votes
3 answers

Cast object to interface in TypeScript

I'm trying to make a cast in my code from the body of a request in express (using body-parser middleware) to an interface, but it's not enforcing type safety. This is my interface: export interface IToDoDto { description: string; status:…
Elias Garcia
  • 6,772
  • 11
  • 34
  • 62
169
votes
9 answers

Do interfaces inherit from Object class in java

Do interfaces inherit from Object class in Java? If no then how we are able to call the method of object class on interface instance public class Test { public static void main(String[] args) { Employee e = null; e.equals(null); …
ponds
  • 2,017
  • 4
  • 16
  • 11
168
votes
2 answers

How do you implement a private setter when using an interface?

I've created an interface with some properties. If the interface didn't exist all properties of the class object would be set to { get; private set; } However, this isn't allowed when using an interface,so can this be achieved and if so how?
dotnetnoob
  • 10,783
  • 20
  • 57
  • 103
167
votes
1 answer

Can extension methods be applied to interfaces?

Is it possible to apply an extension method to an interface? (C# question) That is for example to achieve the following: create an ITopology interface create an extension method for this interface (e.g. public static int CountNodes(this ITopology…
Greg
  • 34,042
  • 79
  • 253
  • 454
163
votes
11 answers

Constructor in an Interface?

I know it's not possible to define a constructor in an interface. But I'm wondering why, because I think it could be very useful. So you could be sure that some fields in a class are defined for every implementation of this interface. For example…
anon
163
votes
2 answers

Go Interface Fields

I'm familiar with the fact that, in Go, interfaces define functionality, rather than data. You put a set of methods into an interface, but you are unable to specify any fields that would be required on anything that implements that interface. For…
Matt Mc
  • 8,882
  • 6
  • 53
  • 89
163
votes
5 answers

Inherit from a generic base class, apply a constraint, and implement an interface in C#

This is a syntax question. I have a generic class which is inheriting from a generic base class and is applying a constraint to one of the type parameters. I also want the derived class to implement an interface. For the life of me, I cannot seem to…
Dan Rigby
  • 17,133
  • 6
  • 43
  • 60
162
votes
8 answers

Polymorphism: Why use "List list = new ArrayList" instead of "ArrayList list = new ArrayList"?

Possible Duplicate: Why should the interface for a Java class be prefered? When should I use List list = new ArrayList(); ArrayList inherits from List, so if some features in ArrayList aren't in List, then I will have lost some…
hqt
  • 29,632
  • 51
  • 171
  • 250