20,121 questions
2783
votes
25
answers
1.1m
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
};
2034
votes
39
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?
1579
votes
34
answers
827k
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 ...
932
votes
33
answers
236k
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 ...
908
votes
18
answers
648k
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?
845
votes
38
answers
208k
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 ...
840
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.
739
votes
16
answers
364k
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'
...
722
votes
30
answers
976k
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;...
692
votes
13
answers
175k
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?...
652
votes
18
answers
459k
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'...
650
votes
8
answers
485k
views
Difference between abstract class and interface in Python
What is the difference between abstract class and interface in Python?
644
votes
16
answers
174k
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 (...
636
votes
15
answers
479k
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 ...
576
votes
24
answers
457k
views
Why can't I define a static method in a Java interface?
NOTE: This question predates Java 8. As of Java 8, static methods are allowed in interfaces. However, they cannot be declared abstract (required to be overridden) in the manner requested by this ...
573
votes
14
answers
373k
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?
558
votes
26
answers
458k
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 ...
549
votes
31
answers
493k
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 ...
505
votes
15
answers
250k
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 ...
456
votes
6
answers
172k
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 ...
445
votes
15
answers
312k
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)
435
votes
4
answers
178k
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 ...
425
votes
7
answers
156k
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 ...
408
votes
18
answers
585k
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 ...
401
votes
13
answers
138k
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....
380
votes
11
answers
148k
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?
375
votes
11
answers
268k
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 ...
363
votes
7
answers
118k
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 ...
349
votes
32
answers
197k
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 ...
346
votes
16
answers
508k
views
Why are interface variables static and final by default?
Why are interface variables static and final by default in Java?
327
votes
12
answers
143k
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 ...
320
votes
9
answers
280k
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();
}
...
296
votes
15
answers
211k
views
Abstract class in Java
What is an "abstract class" in Java?
285
votes
12
answers
236k
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 ...
257
votes
13
answers
515k
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 ...
257
votes
7
answers
201k
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
104k
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 ...
243
votes
18
answers
182k
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 ...
242
votes
10
answers
85k
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 ...
229
votes
8
answers
163k
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 ...
220
votes
9
answers
136k
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 an ...
218
votes
11
answers
178k
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 ...
213
votes
24
answers
31k
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: ...
213
votes
2
answers
139k
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 {
...
213
votes
6
answers
41k
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);
...
211
votes
4
answers
144k
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 ...
204
votes
8
answers
145k
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?
199
votes
6
answers
148k
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)?
199
votes
21
answers
222k
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?
199
votes
8
answers
96k
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?