Questions tagged [interfaces]

Questions about `interface` related design considerations, and also "programming to the interface instead of the implementation"

Popular questions and answers

About the difference between "programming to an interface" (which might use Java/C#-style interfaces) and Java/C#-style interfaces:
External: Program to an interface not an Interface

711 questions
326
votes
15 answers

Do I need to use an interface when only one class will ever implement it?

Isn't the whole point of an interface that multiple classes adhere to a set of rules and implementations?
Lamin Sanneh
  • 3,975
  • 3
  • 19
  • 19
172
votes
19 answers

Why are interfaces useful?

I have been studying and coding in C# for some time now. But still, I can't figure the usefulness of Interfaces. They bring too little to the table. Other than providing the signatures of function, they do nothing. If I can remember the names and…
Pankaj Upadhyay
  • 5,060
  • 11
  • 44
  • 60
169
votes
9 answers

I changed one method signature and now have over 25,000 errors. What now?

I started a new job recently where I am working on a very large application (15M loc). In my previous job we had a similarly large application but (for better or for worse) we used OSGi, which meant the application was broken down into lots of…
user788497
  • 1,321
  • 2
  • 9
  • 6
145
votes
6 answers

What is the point of having every service class have an interface?

At the company I work at, every service class has a corresponding interface. Is this necessary? Notes: Most of these interfaces are only used by a single class We are not creating any sort of public API With modern mocking libraries able to…
Bob Roberts
  • 1,777
  • 2
  • 11
  • 12
136
votes
5 answers

Why should I prefer composition over inheritance?

I always read that composition is to be preferred over inheritance. A blog post on unlike kinds, for example, advocates using composition over inheritance, but I can't see how polymorphism is achieved. But I have a feeling that when people say…
MustafaM
  • 1,958
  • 3
  • 15
  • 13
111
votes
5 answers

Why were default and static methods added to interfaces in Java 8 when we already had abstract classes?

In Java 8, interfaces can contain implemented methods, static methods, and the so-called "default" methods (which the implementing classes do not need to override). In my (probably naive) view, there was no need to violate interfaces like this.…
Mister Smith
  • 2,867
  • 4
  • 21
  • 17
98
votes
8 answers

Should interface names begin with an "I" prefix?

I have been reading "Clean Code" by Robert Martin to hopefully, become a better programmer. While none of it so far has been really ground breaking it has made me think differently about the way I design applications and write code. There is one…
Charles Sprayberry
  • 1,606
  • 1
  • 12
  • 21
96
votes
12 answers

What is meant by the phrase “Software can replace hardware”?

Studying beginners course on hardware/software interface and operating systems, often come up the topic of if it would be better to replace some hardware parts with software and vice-versa. I can't make the connection.
Gabriele Scarlatti
  • 1,107
  • 1
  • 8
  • 8
95
votes
15 answers

Should we design our code from the beginning to enable unit testing?

There's a debate going on in our team at the moment as to whether modifying code design to allow unit testing is a code smell, or to what extent it can be done without being a code smell. This has come about because we're only just starting to put…
Lee
  • 1,101
  • 1
  • 8
  • 11
72
votes
7 answers

When to use abstract classes instead of interfaces with extension methods in C#?

"Abstract class" and "interface" are similar concepts, with interface being the more abstract of the two. One differentiating factor is that abstract classes provide method implementations for derived classes when needed. In C#, however, this…
Gulshan
  • 9,402
  • 10
  • 58
  • 89
64
votes
10 answers

Is better Show() + Hide() or SetVisible(bool visible)?

What is better and why? (From interface-design point of view) : a) To have two Show() and Hide() functions b) To have one SetVisible(bool visible) function EDIT: For example some object have visibility state and this functions is used to change…
user3123061
  • 1,609
  • 1
  • 15
  • 17
59
votes
3 answers

Why does C# allow properties in interfaces?

In C#, the following code is valid interface I{ int property{get;set;} } Which doesn't make any sense to me. This seems to break one of the most important principles of interfaces: lack of state (in other words, no fields). Doesn't the…
58
votes
8 answers

How do I ensure that interface implementations are implemented in the manner I expected?

Let's say there is a member SomeMethod in an interface ISomeInterface as follows: public interface ISomeInterface { int SomeMethod(string a); } For the purposes of my program, all consumers of ISomeInterface act upon the assumption that the…
user4779
  • 929
  • 1
  • 6
  • 13
47
votes
5 answers

What did Rich Hickey mean when he said, "All that specificity [of interfaces/classes/types] kills your reuse!"

In Rich Hickey's thought-provoking goto conference keynote "The Value of Values" at 29 minutes he's talking about the overhead of a language like Java and makes a statement like, "All those interfaces kill your reuse." What does he mean? Is that…
GlenPeterson
  • 14,890
  • 6
  • 47
  • 75
45
votes
10 answers

Why use an interface when the class can directly implement the functions?

Possible Duplicate: Why are interfaces useful? Like most faculty, my java faculty introduced interface without explaining or even mentioning its practical use. Now I imagine interfaces have a very specific use, but can't seem to find the…
SoWhat
  • 561
  • 1
  • 5
  • 8
1
2 3
47 48