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
2418
votes
27 answers

Interfaces vs Types in TypeScript

What is the difference between these statements (interface vs type) in TypeScript? interface X { a: number b: string } type X = { a: number b: string };
user6101582
1993
votes
38 answers

What is the difference between an interface and abstract class?

What exactly is the difference between an interface and an abstract class?
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
1550
votes
34 answers

Interface vs Abstract Class (general OO)

I have recently had two telephone interviews where I've been asked about the differences between an Interface and an Abstract class. I have explained every aspect of them I could think of, but it seems they are waiting for me to mention something…
Houman
  • 64,245
  • 87
  • 278
  • 460
914
votes
33 answers

What does it mean to "program to an interface"?

I have seen this mentioned a few times and I am not clear on what it means. When and why would you do this? I know what interfaces do, but the fact I am not clear on this makes me think I am missing out on using them correctly. Is it just so if…
Damien
  • 13,927
  • 14
  • 55
  • 88
883
votes
17 answers

How do you declare an interface in C++?

How do I setup a class that represents an interface? Is this just an abstract base class?
Aaron Fischer
  • 20,853
  • 18
  • 75
  • 116
835
votes
38 answers

Interface vs Base class

When should I use an interface and when should I use a base class? Should it always be an interface if I don't want to actually define a base implementation of the methods? If I have a Dog and Cat class. Why would I want to implement IPet instead…
Howler
  • 2,252
  • 6
  • 22
  • 27
810
votes
19 answers

Implements vs extends: When to use? What's the difference?

Please explain in an easy to understand language or a link to some article.
Saad Masood
  • 11,036
  • 9
  • 32
  • 40
706
votes
16 answers

How to determine if a type implements an interface with C# reflection

Does reflection in C# offer a way to determine if some given System.Type type models some interface? public interface IMyInterface {} public class MyType : IMyInterface {} // should yield 'true' typeof(MyType)./* ?????…
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
682
votes
13 answers

C# Interfaces. Implicit implementation versus Explicit implementation

What are the differences in implementing interfaces implicitly and explicitly in C#? When should you use implicit and when should you use explicit? Are there any pros and/or cons to one or the other? Microsoft's official guidelines (from first…
Seb Nilsson
  • 26,200
  • 30
  • 103
  • 130
636
votes
8 answers

Difference between abstract class and interface in Python

What is the difference between abstract class and interface in Python?
gizmo
  • 7,989
  • 6
  • 23
  • 22
631
votes
28 answers

Interface type check with Typescript

This question is the direct analogon to Class type check with TypeScript I need to find out at runtime if a variable of type any implements an interface. Here's my code: interface A{ member:string; } var a:any={member:"foobar"}; if(a…
lhk
  • 27,458
  • 30
  • 122
  • 201
630
votes
15 answers

Type List vs type ArrayList in Java

(1) List myList = new ArrayList(); (2) ArrayList myList = new ArrayList(); I understand that with (1), implementations of the List interface can be swapped. It seems that (1) is typically used in an application regardless of need…
kji
  • 6,681
  • 3
  • 18
  • 7
629
votes
16 answers

Interface defining a constructor signature?

It's weird that this is the first time I've bumped into this problem, but: How do you define a constructor in a C# interface? Edit Some people wanted an example (it's a free time project, so yes, it's a game) IDrawable +Update +Draw To be able to…
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
629
votes
16 answers

When to use: Java 8+ interface default method, vs. abstract method

Java 8 allows for default implementation of methods in interfaces called Default Methods. I am confused between when would I use that sort of interface default method, instead of an abstract class (with abstract method(s)). So when should interface…
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
565
votes
14 answers

The difference between the Runnable and Callable interfaces in Java

What is the difference between using the Runnable and Callable interfaces when designing a concurrent thread in Java, why would you choose one over the other?
Scottm
  • 7,004
  • 8
  • 32
  • 33
1
2 3
99 100