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.
interface
20,070
questions
2540
votes
24
answers
890k
views
Interfaces vs Types in TypeScript
What is the difference between these statements (interface vs type) in TypeScript?
interface X {
a: number
b: string
}
type X = {
a: number
b: string
};
2006
votes
38
answers
1.4m
views
What is the difference between an interface and abstract class?
What exactly is the difference between an interface and an abstract class?
1558
votes
34
answers
785k
views
Interface vs Abstract Class (general OO)
I have recently had 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 ...
922
votes
33
answers
226k
views
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 ...
891
votes
17
answers
602k
views
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?
838
votes
38
answers
198k
views
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 ...
818
votes
19
answers
1.3m
views
Implements vs extends: When to use? What's the difference?
Please explain in an easy to understand language or a link to some article.
717
votes
16
answers
339k
views
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'
...
682
votes
13
answers
170k
views
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?...
671
votes
30
answers
845k
views
Interface type check with Typescript
This question is the direct analogon to Class type check in TypeScript
I need to find out at runtime if a variable of type any implements an interface. Here's my code:
interface A {
member: string;...
642
votes
18
answers
435k
views
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'...
641
votes
8
answers
472k
views
Difference between abstract class and interface in Python
What is the difference between abstract class and interface in Python?
635
votes
15
answers
469k
views
Type List vs type ArrayList in Java [duplicate]
(1) List<?> myList = new ArrayList<?>();
(2) ArrayList<?> myList = new ArrayList<?>();
I understand that with (1), implementations of the List interface can be swapped. It ...
633
votes
16
answers
166k
views
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 (...
568
votes
14
answers
365k
views
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?
565
votes
24
answers
433k
views
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<T>
{
static T newInstanceFromXML(Element e);
Element toXMLElement();
}
Of ...
540
votes
31
answers
478k
views
How should I have explained the difference between an Interface and an Abstract class? [closed]
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 ...
535
votes
25
answers
422k
views
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 ...
491
votes
15
answers
234k
views
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 ...
433
votes
15
answers
294k
views
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)
427
votes
6
answers
156k
views
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 ...
416
votes
7
answers
146k
views
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 ...
412
votes
4
answers
153k
views
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 ...
403
votes
18
answers
554k
views
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 ...
394
votes
13
answers
133k
views
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....
374
votes
11
answers
140k
views
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?
370
votes
11
answers
258k
views
Interface naming in Java [closed]
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 ...
351
votes
7
answers
107k
views
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 ...
335
votes
16
answers
486k
views
Why are interface variables static and final by default?
Why are interface variables static and final by default in Java?
333
votes
32
answers
180k
views
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 ...
323
votes
12
answers
134k
views
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 ...
309
votes
8
answers
263k
views
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();
}
...
297
votes
15
answers
209k
views
Abstract class in Java
What is an "abstract class" in Java?
282
votes
12
answers
218k
views
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 ...
254
votes
13
answers
497k
views
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 ...
253
votes
7
answers
186k
views
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 {
...
244
votes
15
answers
102k
views
What is the point of interfaces in PHP?
Interfaces allow you to create code that 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 ...
242
votes
11
answers
84k
views
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 ...
239
votes
18
answers
173k
views
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 ...
225
votes
8
answers
152k
views
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 ...
218
votes
9
answers
131k
views
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 ...
213
votes
24
answers
29k
views
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: ...
212
votes
5
answers
39k
views
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);
...
210
votes
11
answers
161k
views
How can I use interface as a C# generic type constraint?
Is there a way to get the following function declaration?
public bool Foo<T>() where T : interface;
ie. where T is an interface type (similar to where T : class, and struct).
Currently I've ...
204
votes
2
answers
122k
views
"<type> 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 {
...
199
votes
7
answers
128k
views
IntelliJ IDEA jump from interface to implementing class in Java
Is there some shortcut that would allow me after creating method in an interface, select and jump to implementing class of that interface?
196
votes
21
answers
216k
views
Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?
Java doesn't allow multiple inheritance, but it allows implementing multiple interfaces. Why?
195
votes
8
answers
88k
views
What does "program to interfaces, not implementations" mean?
One stumbles upon this phrase when reading about design patterns.
But I don't understand it, could someone explain this for me?
194
votes
6
answers
138k
views
Checking if an instance's class implements an interface?
Given a class instance, is it possible to determine if it implements a particular interface? As far as I know, there isn't a built-in function to do this directly. What options do I have (if any)?
190
votes
4
answers
117k
views
When use a interface or class in Typescript [duplicate]
I have a simple scenario of a login that requires user's input a email and password in Typescript. I need to create some type to get this strong-typed and send it to the back-end.
Should this be ...