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
146
votes
4 answers

Why would one declare a Java interface method as abstract?

I used the "pull interface" refactoring feature of Eclipse today to create an interface based on an existing class. The dialog box offered to create all the new methods of the new interface as "abstract" methods. What would be the benefit of that? I…
Uri
  • 88,451
  • 51
  • 221
  • 321
143
votes
7 answers

Why an abstract class implementing an interface can miss the declaration/implementation of one of the interface's methods?

A curious thing happens in Java when you use an abstract class to implement an interface: some of the interface's methods can be completely missing (i.e. neither an abstract declaration or an actual implementation is present), but the compiler does…
Giulio Piancastelli
  • 15,368
  • 5
  • 42
  • 62
142
votes
9 answers

Difference between interfaces and classes in Typescript

What is the different between Typescript Interfaces and Classes? When do I use a Class? When do I use Interfaces? What are the advantages of them? I need to create some kind of types for an http-request to my backend server (Doing it with Angular…
Emre Öztürk
  • 2,660
  • 4
  • 16
  • 19
140
votes
9 answers

Find Java classes implementing an interface

Some time ago, I came across a piece of code, that used some piece of standard Java functionality to locate the classes that implemented a given interface. I know the functions were hidden in some non-logical place, but they could be used for other…
Linor
  • 1,850
  • 2
  • 17
  • 17
140
votes
13 answers

Optional Methods in Java Interface

From my understanding if you implement an interface in java, the methods specified in that interface have to be used by the sub classes implementing the said interface. I've noticed that in some interfaces such as the Collection interface there are…
mjsey
  • 1,938
  • 5
  • 18
  • 28
138
votes
10 answers

Why does Eclipse complain about @Override on interface methods?

I have an existing project that uses @Override on methods that override interface methods, rather than superclass methods. I cannot alter this in code, but I would like Eclpse to stop complaining about the annotation, as I can still build with…
Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
136
votes
12 answers

What is the difference between static and default methods in a Java interface?

I was learning through interfaces when I noticed that you can now define static and default methods in an interface. public interface interfacesample2 { public static void method() { System.out.println("hello world"); } public…
Vipin Menon
  • 2,892
  • 4
  • 20
  • 35
134
votes
10 answers

Java's Interface and Haskell's type class: differences and similarities?

While I am learning Haskell, I noticed its type class, which is supposed to be a great invention that originated from Haskell. However, in the Wikipedia page on type class: The programmer defines a type class by specifying a set of function or …
zw324
  • 26,764
  • 16
  • 85
  • 118
132
votes
4 answers

Is it possible to use getters/setters in interface definition?

At the moment, TypeScript does not allow use get/set methods(accessors) in interfaces. For example: interface I { get name():string; } class C implements I { get name():string { return null; } } furthermore, TypeScript…
Ivan Popov
  • 1,331
  • 2
  • 8
  • 5
129
votes
4 answers

How to extend a class in python?

In python how can you extend a class? For example if I have color.py class Color: def __init__(self, color): self.color = color def getcolor(self): return self.color color_extended.py import Color class Color: def…
omega
  • 40,311
  • 81
  • 251
  • 474
126
votes
6 answers

Is there a way to create interfaces in ES6 / Node 4?

ES6 is fully available in Node 4. I was wondering whether it includes a concept of interface to define method contracts as in MyClass implements MyInterface. I can't find much with my Googling, but maybe there is a nice trick or workaround…
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
124
votes
11 answers

Why implement interface explicitly?

So, what exactly is a good use case for implementing an interface explicitly? Is it only so that people using the class don't have to look at all those methods/properties in intellisense?
Master Morality
  • 5,837
  • 6
  • 31
  • 43
122
votes
16 answers

What is the difference between an interface and a class, and why I should use an interface when I can implement the methods directly in the class?

I am aware that this is a very basic question, but an interviewer asked me in a very trick way and I was helpless :( I know only material or theoretical definition for an interface and also implemented it in many projects I worked on. But I really…
Jasmine
  • 5,186
  • 16
  • 62
  • 114
121
votes
10 answers

What is java interface equivalent in Ruby?

Can we expose interfaces in Ruby like we do in java and enforce the Ruby modules or classes to implement the methods defined by interface. One way is to use inheritance and method_missing to achieve the same but is there any other more appropriate…
crazycrv
  • 2,395
  • 2
  • 19
  • 20
121
votes
7 answers

Should Javadoc comments be added to the implementation?

Is it correct practice to add Javadoc comments in the interface and add non-Javadoc comments in the implementation? Most IDEs generate non-JavaDoc comments for implementations when you auto generate comments. Shouldn't the concrete method have the…
Vaishak Suresh
  • 5,735
  • 10
  • 41
  • 66