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
323
votes
12 answers

Should methods in a Java interface be declared with or without a public access modifier?

Should methods in a Java interface be declared with or without the public access modifier? Technically it doesn't matter, of course. A class method that implements an interface is always public. But what is a better convention? Java itself is not…
Benno Richters
  • 15,378
  • 14
  • 42
  • 45
302
votes
8 answers

Implementing two interfaces in a class with same method. Which interface method is overridden?

Two interfaces with same method names and signatures. But implemented by a single class then how the compiler will identify the which method is for which interface? Ex: interface A{ int f(); } interface B{ int f(); } class Test implements A,…
Jothi
  • 14,720
  • 22
  • 68
  • 93
297
votes
15 answers

Abstract class in Java

What is an "abstract class" in Java?
keyur
278
votes
12 answers

Why can't C# interfaces contain fields?

For example, suppose I want an ICar interface and that all implementations will contain the field Year. Does this mean that every implementation has to separately declare Year? Wouldn't it be nicer to simply define this in the interface?
deltanovember
  • 42,611
  • 64
  • 162
  • 244
252
votes
13 answers

Multiple Inheritance in C#

Since multiple inheritance is bad (it makes the source more complicated) C# does not provide such a pattern directly. But sometimes it would be helpful to have this ability. For instance I'm able to implement the missing multiple inheritance pattern…
Martin
  • 10,738
  • 14
  • 59
  • 67
244
votes
7 answers

When to use Interface and Model in TypeScript / Angular

I recently watched a Tutorial on Angular 2 with TypeScript, but unsure when to use an Interface and when to use a Model for data structures. Example of interface: export interface IProduct { ProductNumber: number; ProductName: string; …
I_LIKE_FOO
  • 2,744
  • 3
  • 16
  • 18
243
votes
15 answers

What is the point of interfaces in PHP?

Interfaces allow you to create code which defines the methods of classes that implement it. You cannot however add any code to those methods. Abstract classes allow you to do the same thing, along with adding code to the method. Now if you can…
mk.
  • 26,076
  • 13
  • 38
  • 41
241
votes
11 answers

Why would a static nested interface be used in Java?

I have just found a static nested interface in our code-base. class Foo { public static interface Bar { /* snip */ } /* snip */ } I have never seen this before. The original developer is out of reach. Therefore I have to ask…
Mo.
  • 15,033
  • 14
  • 47
  • 57
237
votes
19 answers

How do you find all subclasses of a given class in Java?

How does one go about and try to find all subclasses of a given class (or all implementors of a given interface) in Java? As of now, I have a method to do this, but I find it quite inefficient (to say the least). The method is: Get a list of all…
Avrom
  • 4,967
  • 2
  • 28
  • 35
218
votes
9 answers

Java abstract interface

Consider an example (which compiles in java) public abstract interface Interface { public void interfacing(); public abstract boolean interfacing(boolean really); } Why is it necessary for an interface to be "declared" abstract? Is there…
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
215
votes
8 answers

How does interfaces with construct signatures work?

I am having some trouble working out how defining constructors in interfaces work. I might be totally misunderstanding something. But I have searched for answers for a good while and I can not find anything related to this. How do I implement the…
Nypan
  • 6,980
  • 3
  • 21
  • 28
212
votes
5 answers

Final arguments in interface methods - what's the point?

In Java, it is perfectly legal to define final arguments in interface methods and do not obey that in the implementing class, e.g.: public interface Foo { public void foo(int bar, final int baz); } public class FooImpl implements Foo { …
mindas
  • 26,463
  • 15
  • 97
  • 154
212
votes
24 answers

How will I know when to create an interface?

I'm at a point in my development learning where I feel like I must learn more about interfaces. I frequently read about them but it just seems like I cannot grasp them. I've read examples like: Animal base class, with IAnimal interface for things…
user53885
  • 3,809
  • 11
  • 33
  • 43
205
votes
11 answers

How can I use interface as a C# generic type constraint?

Is there a way to get the following function declaration? public bool Foo() where T : interface; ie. where T is an interface type (similar to where T : class, and struct). Currently I've settled for: public bool Foo() where T : IBase; Where…
Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
199
votes
2 answers

" is pointer to interface, not interface" confusion

I have this problem which seems a bit weird to me. Take a look at this snippet of code: package coreinterfaces type FilterInterface interface { Filter(s *string) bool } type FieldFilter struct { Key string Val string } func (ff…
0rka
  • 2,246
  • 2
  • 11
  • 20