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.

18280 questions
1835
votes
37 answers

What is the difference between an interface and abstract class?

What exactly is the difference between an interface and abstract class?
Sarfraz
  • 355,543
  • 70
  • 511
  • 562
1475
votes
35 answers

Interface vs Abstract Class (general OO)

I have had recently 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
  • 58,044
  • 75
  • 235
  • 407
847
votes
32 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,187
  • 14
  • 53
  • 86
828
votes
16 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
  • 19,913
  • 17
  • 69
  • 108
791
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,192
  • 6
  • 21
  • 26
755
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
  • 10,178
  • 8
  • 29
  • 39
654
votes
12 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
  • 24,850
  • 29
  • 95
  • 124
621
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
  • 20,062
  • 23
  • 85
  • 143
589
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,281
  • 3
  • 16
  • 7
588
votes
15 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
  • 38,384
  • 18
  • 73
  • 117
581
votes
19 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
  • 82,870
  • 79
  • 201
  • 297
579
votes
8 answers

Difference between abstract class and interface in Python

What is the difference between abstract class and interface in Python?
gizmo
  • 7,229
  • 6
  • 21
  • 21
525
votes
24 answers

Why can't I define a static method in a Java interface?

EDIT: As of Java 8, static methods are now allowed in interfaces. Here's the example: public interface IXMLizable { static T newInstanceFromXML(Element e); Element toXMLElement(); } Of course this won't work. But why not? One of the…
cdmckay
  • 30,040
  • 21
  • 77
  • 113
518
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
  • 6,381
  • 6
  • 30
  • 32
498
votes
31 answers

How should I have explained the difference between an Interface and an Abstract class?

In one of my interviews, I have been asked to explain the difference between an Interface and an Abstract class. Here's my response: Methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can…
Thinker
  • 6,342
  • 9
  • 23
  • 46
1
2 3
99 100