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
560
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
  • 31,832
  • 25
  • 83
  • 114
538
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,820
  • 9
  • 27
  • 54
528
votes
25 answers

When to use an interface instead of an abstract class and vice versa?

This may be a generic OOP question. I wanted to do a generic comparison between an interface and an abstract class on the basis of their usage. When would one want to use an interface and when would one want to use an abstract class?
Chirantan
  • 15,304
  • 8
  • 49
  • 75
484
votes
15 answers

Should we @Override an interface's method implementation?

Should a method that implements an interface method be annotated with @Override? The javadoc of the Override annotation says: Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is…
Benno Richters
  • 15,378
  • 14
  • 42
  • 45
419
votes
14 answers

Test if object implements interface

What is the simplest way of testing if an object implements a given interface in C#? (Answer to this question in Java)
JoshRivers
  • 9,920
  • 8
  • 39
  • 39
417
votes
6 answers

What's the difference between interface and @interface in java?

I haven't touched Java since using JBuilder in the late 90's while at University, so I'm a little out of touch - at any rate I've been working on a small Java project this week, and using Intellij IDEA as my IDE, for a change of pace from my regular…
Bittercoder
  • 11,753
  • 10
  • 58
  • 76
414
votes
7 answers

Why are C# 4 optional parameters defined on interface not enforced on implementing class?

I noticed that with the optional parameters in C# 4 if you specify an optional parameter on an interface you don't have to make that parameter optional on any implementing class: public interface MyInterface { void TestMethod(bool flag =…
theburningmonk
  • 15,701
  • 14
  • 61
  • 104
397
votes
18 answers

Java Pass Method as Parameter

I am looking for a way to pass a method by reference. I understand that Java does not pass methods as parameters, however, I would like to get an alternative. I've been told interfaces are the alternative to passing methods as parameters but I…
Mac
394
votes
13 answers

Traits vs. interfaces

I've been trying to study up on PHP lately, and I find myself getting hung up on traits. I understand the concept of horizontal code reuse and not wanting to necessarily inherit from an abstract class. What I don't understand is: What is the crucial…
datguywhowanders
  • 4,341
  • 6
  • 20
  • 14
393
votes
4 answers

X does not implement Y (... method has a pointer receiver)

There are already several Q&As on this "X does not implement Y (... method has a pointer receiver)" thing, but to me, they seems to be talking about different things, and not applying to my specific case. So, instead of making the question very…
xpt
  • 20,363
  • 37
  • 127
  • 216
370
votes
11 answers

Interface or an Abstract Class: which one to use?

Please explain when I should use a PHP interface and when I should use an abstract class? How I can change my abstract class in to an interface?
user220758
367
votes
11 answers

Interface naming in Java

Most OO languages prefix their interface names with a capital I, why does Java not do this? What was the rationale for not following this convention? To demonstrate what I mean, if I wanted to have a User interface and a User implementation I'd…
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
341
votes
7 answers

Explicitly calling a default method in Java

Java 8 introduces default methods to provide the ability to extend interfaces without the need to modify existing implementations. I wonder if it's possible to explicitly invoke the default implementation of a method when that method has been…
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
333
votes
32 answers

Interfaces — What's the point?

The reason for interfaces truly eludes me. From what I understand, it is kind of a work around for the non-existent multi-inheritance which doesn't exist in C# (or so I was told). All I see is, you predefine some members and functions, which then…
Nebelhom
  • 3,783
  • 4
  • 21
  • 22
332
votes
16 answers

Why are interface variables static and final by default?

Why are interface variables static and final by default in Java?
Jothi
  • 14,720
  • 22
  • 68
  • 93