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
162
votes
13 answers

What is the use of interface constants?

I am learning Java and just found that the Interface can have fields, which are public static and final. I haven't seen any examples of these so far. What are some of the use cases of these Interface Constants and can I see some in the Java Standard…
unj2
  • 52,135
  • 87
  • 247
  • 375
162
votes
5 answers

Java8: Why is it forbidden to define a default method for a method from java.lang.Object

Default methods are a nice new tool in our Java toolbox. However, I tried to write an interface that defines a default version of the toString method. Java tells me that this is forbidden, since methods declared in java.lang.Object may not be…
gexicide
  • 38,535
  • 21
  • 92
  • 152
160
votes
3 answers

C#: Abstract classes need to implement interfaces?

My test code in C#: namespace DSnA { public abstract class Test : IComparable { } } Results in the following compiler error: error CS0535: 'DSnA.Test' does not implement interface member 'System.IComparable.CompareTo(object)' Since…
bguiz
  • 27,371
  • 47
  • 154
  • 243
159
votes
17 answers

Is there more to an interface than having the correct methods

So lets say I have this interface: public interface IBox { public void setSize(int size); public int getSize(); public int getArea(); //...and so on } And I have a class that implements it: public class Rectangle implements IBox { …
Ali
  • 261,656
  • 265
  • 575
  • 769
159
votes
4 answers

In C#, can a class inherit from another class and an interface?

I want to know if a class can inherit from a class and an interface. The example code below doesn't work but I think it conveys what I want to do. The reason that I want to do this is because at my company we make USB, serial, Ethernet, etc device.…
PICyourBrain
  • 9,976
  • 26
  • 91
  • 136
158
votes
9 answers

Comment the interface, implementation or both?

I imagine that we all (when we can be bothered!) comment our interfaces. e.g. /// /// Foo Interface /// public interface Foo { /// /// Will 'bar' /// /// Wibble…
ng5000
  • 12,330
  • 10
  • 51
  • 64
157
votes
12 answers

How can I implement static methods on an interface?

I have a 3rd party C++ DLL that I call from C#. The methods are static. I want to abstract it out to do some unit testing so I created an interface with the static methods in it but now my program errors with: The modifier 'static' is not valid for…
Jon
  • 38,814
  • 81
  • 233
  • 382
157
votes
15 answers

Protected in Interfaces

Why are all methods in an interface definition implicitly public? Why does it not allow a protected method?
Swaranga Sarma
  • 13,055
  • 19
  • 60
  • 93
157
votes
8 answers

What is the purpose of the default keyword in Java?

An interface in Java is similar to a class, but the body of an interface can include only abstract methods and final fields (constants). Recently, I saw a question, which looks like this interface AnInterface { public default void…
Ravi
  • 30,829
  • 42
  • 119
  • 173
154
votes
16 answers

What is the definition of "interface" in object oriented programming

A friend of mine goes back and forth on what "interface" means in programming. What is the best description of an "interface"? To me, an interface is a blueprint of a class. Is this the best definition?
Daniel Kivatinos
  • 24,088
  • 23
  • 61
  • 81
154
votes
14 answers

Why can't I declare static methods in an interface?

The topic says the most of it - what is the reason for the fact that static methods can't be declared in an interface? public interface ITest { public static String test(); } The code above gives me the following error (in Eclipse, at least):…
Henrik Paul
  • 66,919
  • 31
  • 85
  • 96
150
votes
4 answers

How to check if an object implements an interface?

How to check if some class implements interface? When having: Character.Gorgon gor = new Character.Gorgon(); how to check if gor implements Monster interface? public interface Monster { public int getLevel(); public int level =…
J.Olufsen
  • 13,415
  • 44
  • 120
  • 185
148
votes
4 answers

Interfaces vs. abstract classes

In C#, when should you use interfaces and when should you use abstract classes? What can be the deciding factor?
tush1r
  • 19,443
  • 14
  • 36
  • 35
147
votes
10 answers

Cast List to List

public interface IDic { int Id { get; set; } string Name { get; set; } } public class Client : IDic { } How can I cast List to List?
Andrew Kalashnikov
  • 3,073
  • 5
  • 34
  • 57
147
votes
11 answers

Jump into interface implementation in Eclipse IDE

You know how in Eclipse, pressing F3 over a method will take you to its declaration? Well I have a method that is part of an interface; clicking F3 over this naturally takes me to the declaring interface. Obviously there is an object implementing…
aeq
  • 10,377
  • 15
  • 45
  • 46